0

我的班级 Point3D 出现错误,我真的不明白为什么。

这是我的标题:

#ifndef POINT3D_H
#define POINT3D_H

using namespace std;
class Point3D {

public:
    Point3D(float x, float y, float z);
    float operator[] (const int i);
private:
    float xyz[3];

};

#endif

这是cpp文件:

#include "point3d.h"

Point3D::Point3D(float x, float y, float z){
    xyz[0] = x;
    xyz[1] = y;
    xyz[2] = z;
}

float Point3D::operator[](int i )
{

    if(i == 0){
        return xyz[0];
    }
    else if(i == 1){
        return xyz[1];
    }
    else if(i == 2){
        return xyz[2];
    }

    return -1;

}

存在编译器错误:

/tmp/ccyDEfcW.o: In function `Point3D::Point3D(float, float, float)':
vector3d.cc:(.text+0x0): multiple definition of `Point3D::Point3D(float, float, float)'
/tmp/ccqDasr3.o:point3d.cc:(.text+0x0): first defined here
/tmp/ccyDEfcW.o: In function `Point3D::Point3D(float, float, float)':
vector3d.cc:(.text+0x10): multiple definition of `Point3D::Point3D(float, float, float)'
/tmp/ccqDasr3.o:point3d.cc:(.text+0x10): first defined here
/tmp/ccyDEfcW.o: In function `Point3D::operator[](int)':
vector3d.cc:(.text+0x20): multiple definition of `Point3D::operator[](int)'
/tmp/ccqDasr3.o:point3d.cc:(.text+0x20): first defined here
collect2: ld returned 1 exit status
4

1 回答 1

1

1)您在标题中有不同的声明:

float operator[] (const int i);

和.cpp中的定义:

float Point3D::operator[](int i )

使它们都具有相同的参数

const int i

2)查看vector3d.cc(请在此处分享),您可能已经定义了两次Point3D代码。

于 2012-09-25T07:10:19.363 回答