gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:04:17 25 4
gpt4 key购买 nike

尝试获取可在 g++ 中编译的代码以在 VS2015 中编译。我环顾了 SO 和谷歌,运气不佳,但 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"

我不明白为什么会抛出这些错误,但是,如果我使用 math.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 中有很多 名称,您可能并不真的想要它们。

关于c++ - 在 Visual Studio 2015 中使用 cmath 时出现大约 200 个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36616787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com