Множественное объявление переполнения стека конструктора

Я получаю ошибки для своего класса 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

0

Решение

1) У вас есть другое объявление в шапке:

float operator[] (const int i);

и определение в .cpp:

float Point3D::operator[](int i )

Сделайте их обоих с одинаковым параметром

const int i

2) Посмотрите на vector3d.cc (и, пожалуйста, поделитесь им здесь),
Вы, вероятно, определили код Point3D дважды.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector