编译时与基类重复符号错误
Duplicate symbol error with base class when compiling
我有一个简单的抽象基类
shape.h
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
Shape(int x, int y) : midx(x), midy(y) {}
virtual ~Shape() = 0;
virtual int area() = 0;
virtual void print() = 0;
protected:
int midx, midy;
};
Shape::~Shape() {}
#endif
和派生类Polygon
polygon.h
#ifndef POLYGON_H
#define POLYGON_H
#include "vertex.h"
#include "shape.h"
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
class Polygon : public Shape
{
public:
Polygon();
Polygon(double x, double y, Vertex *arr, int size);
Polygon(const Polygon& pol);
~Polygon();
.....
.....
int area(); //overloading base class
void print(); //overloading base class
// friends
friend ostream& operator<<(ostream& out, const Polygon& pol)
{
out << "(" << pol.midx << "," << pol.midy << ") { ";
for(int i=0; i<pol.numVertices(); i++) {
out << pol[i];
}
return out << " }";
}
private:
Vertex *vertex_arr;
int vertices;
};
#endif
polygon.cpp
#include "polygon.h"
Polygon::Polygon() : Shape(0,0)
{
vertex_arr = 0;
vertices = 0;
}
Polygon::Polygon(double x, double y, Vertex *arr, int size) : Shape(x,y)
{
vertex_arr = new Vertex[size];
copy(arr, arr+size, vertex_arr);
vertices = size;
}
Polygon::Polygon(const Polygon& pol) : Shape(0,0)
{
vertices = pol.vertices;
vertex_arr = new Vertex[vertices];
copy(pol.vertex_arr, pol.vertex_arr+vertices, vertex_arr);
}
Polygon::~Polygon()
{
delete [] vertex_arr;
}
.....
.....
int Polygon::area()
{
return 1;
}
void Polygon::print()
{
std::cout << "(" << midx << "," << midy << ") { ";
for(int i=0; i<numVertices(); i++) {
std::cout << vertex_arr[i];
}
std::cout << " }" << std::endl;
}
main.cpp
#include "polygon.h"
#include <iostream>
using namespace std;
int main()
{
Vertex varr[] = { Vertex(0,0), Vertex(10,0),
Vertex(5,2), Vertex(5,5) };
Polygon *p = new Polygon( 1, 4, varr, 4 );
cout << *p << endl;
cout << "Now the print() method" << endl;
p->print();
return 0;
}
当我编译我的cpp文件(多边形和主),我得到以下错误
duplicate symbol __ZN5ShapeD0Ev in:
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/main-b44100.o
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/polygon-a07778.o
duplicate symbol __ZN5ShapeD1Ev in:
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/main-b44100.o
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/polygon-a07778.o
duplicate symbol __ZN5ShapeD2Ev in:
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/main-b44100.o
/var/folders/lk/5y917sd55lx3hbp_5r5vmklc0000gn/T/polygon-a07778.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我做错了什么?我是c++新手,所以…由于
您已经将~Shape()
的定义放在类定义之外,但在头文件中。这样就产生了多个定义,每个翻译单元中都有一个定义,其中包括头文件。您有四个选项:
- 标记为
inline
- 将定义放在类定义中(这使得它隐式地
inline
) - 将定义放入实现(
.cpp
)文件 - 省略析构函数:它什么都不做,所以编译器生成一个就足够了。
相关文章:
- 在C++中使用内联方法时出现未定义的符号错误
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 函数指针给我"重新声明为不同类型的符号"错误
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 函数路径追加上未解决的外部符号错误
- 作为成员的静态类模板实例会导致未解析的外部符号错误
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 共享库中使用"使用命名空间"定义的符号错误
- 在FreesWitch中使用AWS C SDK加载模块时未定义的符号错误
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- C++编译错误LNK2019:未解决的外部符号错误
- VTK / ITK/ QT - 未解决的外部符号错误 (LNK2001)
- QT创建者:简单程序未解决的外部符号和外部符号错误
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 由于使用未签名的INT,因此在Variadic模板C 上未解决的外部符号错误
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- 使用从WINRT网格类派生的抽象类时,Casttobase未解决的外部符号错误
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See