尝试获取在g++中可编译的代码,以便在VS2015中编译。我环顾四周&谷歌并没有多少运气,但在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 & 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);
}发布于 2016-04-14 08:29:40
之间的区别
#include <math.h>和
#include <cmath>前者将像sqrt和pow这样的东西放入全局命名空间中(即,您只通过说sqrt或pow来引用它们),而后者将它们放入名称空间std中(即,您通过表示std::sqrt或std::pow来引用它们)。
如果您不想一直用std::作为前缀,可以显式地将单个名称空间放在全局命名空间中:
using std::sqrt;或者(虽然不推荐这样做),您可以像这样拉进整个std:
using namespace std;问题在于,std中有很多名称,而且您可能并不完全想要它们。
https://stackoverflow.com/questions/36616787
复制相似问题