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

About 200 Errors When Using cmath in Visual Studio 2015

本文关键字:错误 200个 cmath Studio Visual 2015      更新时间:2023-10-16

试图获得在g++中可编译的代码以在VS2015中编译。我环顾四周;谷歌运气不太好,但cmath在MSDN中有文档记录。我想我错过了一些非常明显或简单的东西。

cmath抛出了很多错误——我在编译过程中遇到的大多数错误,其中一半的形式是:

the global scope has no "<function>"

其他是形式

'<function>': redefinition; different exception specification
'<function>': identifier not found
'<function>': is not a member of "global namespace"

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

编辑:根据要求,代码。我正在使用sqrt&pow函数:

#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);
}

之间的差异

#include <math.h>

#include <cmath>

前者将sqrtpow之类的东西放入全局名称空间(即,仅通过说sqrtpow来引用它们),而后者将它们放入名称空间std(即,通过说std::sqrtstd::pow来引用它们。

如果你不想一直用std::作为前缀,你可以显式地将单个放在全局命名空间中:

using std::sqrt;

或者(尽管不建议这样做)你可以像这样拉入整个std

using namespace std;

问题是std中有很多名称,您可能并不真的想要全部。

相关文章: