Duplicate symbol error with base class when compiling

c++

Solution

You have placed the definition of `~Shape()` outside of the class definition, but inside the header. That results in multiple definitions, one in each translation unit that includes the header. You have four options:

- Mark it `inline`

- Put the definition inside of the class definition (this makes it implicitly `inline`.)

- Put the definition in an implementation (`.cpp`) file

- Omit the destructor: it does nothing, so the compiler generated one will suffice.

Problem

I have a simple abstract base class 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 ``` And a derived class 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; } ``` When I compile my cpp files (polygon and main) I get the following error ``` 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) ``` What am I doing wrong? Im quite new to C++ so... Thanks

Original source