linux - c++中类函数定义与声明问题
PHPz
PHPz 2017-04-17 16:59:25
0
1
790

对运算符*/重载,当把这两种重载函数都在.h文件中定义时,不报错,当把他们在.cpp文件实现时,提示:“vector.cpp:44:37: error: no ‘Vector3 Vector3::operator/(float)’ member function declared in class ‘Vector3’
Vector3 Vector3::operator/(float num)”。

vector.h

/*实现Vector3(三维向量)相关运算的类*/
#ifndef VECTOR_3
#define VECTOR_3

#include <iostream>
#include <algorithm>
using namespace std;
class Vector3
{
public:
        /*缺省的构造函数*/
        Vector3()
        {
                x = 0;
                y = 0;
                z = 0;
        }
        Vector3(float x, float y, float z);
        ~Vector3(){cout<<"执行析构函数" <<endl;}
        
        float getX() const {return x;}
        float getY() const {return y;}
        float getZ() const {return z;};
        
        /*三维向量的相关运算*/
        void set(float _x,float _y,float _z);
        Vector3 operator+(const Vector3 &v);
        Vector3 operator-(const Vector3 &v);
        Vector3 operator*(float num);
        Vector3 operator/(float num);
        
        
        friend ostream &operator<<(ostream &out,Vector3 &v)
        {
                out<<"("<<v.x<<","<<v.y<<","<<v.z<<")"<<endl;
                return out;
        }
private:
        float x;
        float y;
        float z;
      
};
Vector3 Vector3::operator*(float num)
{
        return Vector3(x*num,y*num,z*num);
}
Vector3 Vector3::operator/(float num)
{
        return Vector3(x/num,y/num,z/num);
}
#endif

vector.cpp

#include "vector.h"  //注意这里,不要yong<>.否则就是去系统中找了

Vector3::Vector3(float x,float y,float z)
{
        this->x = x;
        this->y = y;
        this->z = z;
}

void Vector3::set(float _x,float _y,float _z)
{
        x = _x;
        y = _y;
        z = _z;
}
Vector3 Vector3::operator+(const Vector3 &v)
{
        return Vector3(x+v.x,y+v.y,z+v.z);
}
Vector3 Vector3::operator-(const Vector3 &v)
{
        return Vector3(x-v.x,y-v.y,z-v.z);
}

int main(int argc,char **argv)
{
        Vector3 v1(1,2,3);
        Vector3 v2(2,3,4);
        Vector3 v3 = v1+v2;
        cout<<"v1 + v2 = "<<v3<<endl;
        
        return 0;
}

运行上述程序不会报错。但是若将vector.h中的

Vector3 Vector3::operator*(float num)
{
        return Vector3(x*num,y*num,z*num);
}
Vector3 Vector3::operator/(float num)
{
        return Vector3(x/num,y/num,z/num);
}

实现部分放到vector.cpp文件中,则会报错!
求解释?
自己所用系统为Gentoo Linux 编译器g++。

当把上述代码移植到window+vs2013时,则没有上述错误,请问为什么??

PHPz
PHPz

学习是最好的投资!

全部回复(1)
PHPzhong

看不出问题在哪里。在Archlinux下使用gcc 6.3.1编译也没有问题。
你把注释都去掉,然后重新编译一次试试。有可能是编码的原因。

Vector.h

/*实现Vector3(三维向量)相关运算的类*/
#ifndef VECTOR_3
#define VECTOR_3

#include <iostream>
#include <algorithm>
using namespace std;
class Vector3
{
public:
        /*缺省的构造函数*/
        Vector3()
        {
                x = 0;
                y = 0;
                z = 0;
        }
        Vector3(float x, float y, float z);
        ~Vector3(){cout<<"执行析构函数" <<endl;}
        
        float getX() const {return x;}
        float getY() const {return y;}
        float getZ() const {return z;};
        
        /*三维向量的相关运算*/
        void set(float _x,float _y,float _z);
        Vector3 operator+(const Vector3 &v);
        Vector3 operator-(const Vector3 &v);
        Vector3 operator*(float num);
        Vector3 operator/(float num);
        
        
        friend ostream &operator<<(ostream &out,Vector3 &v)
        {
                out<<"("<<v.x<<","<<v.y<<","<<v.z<<")"<<endl;
                return out;
        }
private:
        float x;
        float y;
        float z;
      
};
#endif

Vector.cpp

#include "Vector.h"

Vector3::Vector3(float x,float y,float z)
{
        this->x = x;
        this->y = y;
        this->z = z;
}

void Vector3::set(float _x,float _y,float _z)
{
        x = _x;
        y = _y;
        z = _z;
}
Vector3 Vector3::operator+(const Vector3 &v)
{
        return Vector3(x+v.x,y+v.y,z+v.z);
}
Vector3 Vector3::operator-(const Vector3 &v)
{
        return Vector3(x-v.x,y-v.y,z-v.z);
}
Vector3 Vector3::operator*(float num)
{
        return Vector3(x*num,y*num,z*num);
}
Vector3 Vector3::operator/(float num)
{
        return Vector3(x/num,y/num,z/num);
}

int main(int argc,char **argv)
{
        Vector3 v1(1,2,3);
        Vector3 v2(2,3,4);
        Vector3 v3 = v1+v2;
        cout<<"v1 + v2 = "<<v3<<endl;
        
        return 0;
}

编译运行结果

/home/x/vec [12:30]
> g++ Vector.cpp 

/home/x/vec [12:31]
> ./a.out 
v1 + v2 = (3,5,7)

执行析构函数
执行析构函数
执行析构函数
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板