在Visual Studio 2015中使用cmath时出现约200个错误
About 200 Errors When Using cmath in Visual Studio 2015
试图获得在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>
前者将sqrt
和pow
之类的东西放入全局名称空间(即,仅通过说sqrt
或pow
来引用它们),而后者将它们放入名称空间std
(即,通过说std::sqrt
或std::pow
来引用它们。
如果你不想一直用std::
作为前缀,你可以显式地将单个放在全局命名空间中:
using std::sqrt;
或者(尽管不建议这样做)你可以像这样拉入整个std
:
using namespace std;
问题是std
中有很多名称,您可能并不真的想要全部。
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 使用命名空间时出现多个定义错误
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- 错误:多个文件中的结构声明冲突
- 唯一的 10 位序列 ID - 200 个 ID/秒
- C++/MySQL,在1次执行中插入200个数据点数组
- 在Visual Studio 2015中使用cmath时出现约200个错误
- 链接器不指出错误;多个定义警告指向同一行
- 在 math.h 中有超过 200 个 ~语法错误~ 对于 Visual Studio 来说,这没有意义
- 生成错误:12 个重载中没有一个可以转换所有参数类型
- visual C++LNK1120致命错误:1个未解析的外部
- 程序特定的OpenGL运行时错误:多个输入缓冲区蒙皮动画