首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在2015中使用cMath时大约有200个错误

在2015中使用cMath时大约有200个错误
EN

Stack Overflow用户
提问于 2016-04-14 07:47:17
回答 1查看 1.4K关注 0票数 0

尝试获取在g++中可编译的代码,以便在VS2015中编译。我环顾四周&谷歌并没有多少运气,但在MSDN中记录了cmath。我猜我错过了一些很明显或很简单的东西。

CMath正抛出许多错误--大部分是我在编译过程中遇到的错误,其中一半是以以下形式出现的:

代码语言:javascript
复制
the global scope has no "<function>"

其他的在形式上

代码语言:javascript
复制
'<function>': redefinition; different exception specification

'<function>': identifier not found

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

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

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

代码语言:javascript
复制
#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

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-14 08:29:40

之间的区别

代码语言:javascript
复制
#include <math.h>

代码语言:javascript
复制
#include <cmath>

前者将像sqrtpow这样的东西放入全局命名空间中(即,您只通过说sqrtpow来引用它们),而后者将它们放入名称空间std中(即,您通过表示std::sqrtstd::pow来引用它们)。

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

代码语言:javascript
复制
using std::sqrt;

或者(虽然不推荐这样做),您可以像这样拉进整个std

代码语言:javascript
复制
using namespace std;

问题在于,std中有很多名称,而且您可能并不完全想要它们。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36616787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档