编译时与基类重复符号错误

Duplicate symbol error with base class when compiling

本文关键字:符号 错误 基类 编译      更新时间:2023-10-16

我有一个简单的抽象基类

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()的定义放在类定义之外,但在头文件中。这样就产生了多个定义,每个翻译单元中都有一个定义,其中包括头文件。您有四个选项:

  1. 标记为inline
  2. 将定义放在类定义中(这使得它隐式地inline)
  3. 将定义放入实现(.cpp)文件
  4. 省略析构函数:它什么都不做,所以编译器生成一个就足够了。
相关文章: