在Visual Studio 2015中使用cmath时大约200个错误

时间:2016-04-14 07:47:17

标签: c++ visual-studio-2015 cmath

尝试获取可在g ++中编译的代码以在VS2015中编译。我看了看周围的SO& Google运气不佳,但MSDN中记录了cmath。我猜测我错过了一些非常明显或简单的东西。

cmath在编译过程中遇到的大部分错误都会造成很多错误,其中一半是以下形式出现的:

the global scope has no "<function>"

其他人的形式为

'<function>': redefinition; different exception specification

'<function>': identifier not found

'<function>': is not a member of "global namespace"

我不明白为什么会抛出这些错误,但是,如果我使用math.h,我的大部分编译错误都会消失(包括其他标准库中的一些错误也会消失)。

编辑:根据要求,代码。我正在使用sqrt&amp;战俘功能:

#include "vector.h"
#include <cmath>

using namespace vectormath;

vector::vector()
{
    this->_x = 0;
    this->_y = 0;
    this->_z = 0;
    this->_length = 0;
}
vector::vector(float x, float y, float z)
{
    this->_x = x;
    this->_y = y;
    this->_z = z;
    this->_length = sqrt(pow(_x, 2) + pow(_y, 2) + pow(_z, 2));
}

vector * vectormath::crossproduct(vector * a, vector * b)
{
    vector * result = new vector();

    result->_x = a->_y * b->_z - a->_z * b->_y;
    result->_y = a->_z * b->_x - a->_x * b->_z;
    result->_z = a->_x * b->_y - a->_y * b->_x;

    return result;
}

point::point()
{
    this->_x = 0.0;
    this->_y = 0.0;
    this->_z = 0.0;
}

point::point(float x, float y, float z)
{
    this->_x = x;
    this->_y = y;
    this->_z = z;
}

float vectormath::dotproduct(vector a, vector b)
{
    return a._x * b._x + a._y * b._y + a._z * b._z;
}

vector * vectormath::add(point * a, vector * b)
{
    vector * c = new vector();

    c->_x = a->_x + b->_x;
    c->_y = a->_y + b->_y;
    c->_z = a->_z + b->_z;

    return c;
}

编辑:和vector.h

namespace vectormath
{
    struct vector
    {
        float _x;
        float _y;
        float _z;
        float _length;

        vector();
        vector(float x, float y, float z);
    };

    struct point
    {
        float _x;
        float _y;
        float _z;

        point();
        point(float x, float y, float z);
    };
    vector * crossproduct(vector*, vector*);
    float dotproduct(vector a, vector b);
    vector * add(point * a, vector * b);
}

1 个答案:

答案 0 :(得分:3)

之间的区别
#include <math.h>

#include <cmath>

前者将sqrtpow之类的内容放入全局命名空间(即,仅通过说sqrtpow来引用它们)将它们放入命名空间std(即,您通过说std::sqrtstd::pow来引用它们。)

如果您不想一直使用std::作为前缀,可以将各个明确地放在全局命名空间中:

using std::sqrt;

或(虽然不建议这样做)你可以像这样引入整个std

using namespace std;

问题在于std中有一个很多的名字,你可能并不真的想要它们。