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

时间:2014-05-21 10:16:21

标签: c++

我有一个简单的抽象基类

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文件(polygon和main)时,我收到以下错误

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 ++的新手,所以...谢谢

1 个答案:

答案 0 :(得分:7)

您已将~Shape()的定义放在类定义之外,但在标题内。这导致多个定义,每个翻译单元中包含一个标题。您有四种选择:

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