构造函数C++的多个声明
Multiple Declaration of Constructor C++
我的类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
1)您在标头中有不同的声明:
float operator[] (const int i);
和.cpp中的定义:
float Point3D::operator[](int i )
使它们具有相同的参数
const int i
2)看看 vector3d.cc(请在这里分享),您可能已经定义了两次 Point3D 代码。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别