加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:52:20 所属栏目:百科 来源:网络整理
导读:我有一个简单的抽象基类 shape.h #ifndef SHAPE_H#define SHAPE_Hclass 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() {}
我有一个简单的抽象基类

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,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,Vertex(5,2),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()的定义放在类定义之外,但在标题内.这导致多个定义,每个翻译单元中包含一个标题.您有四种选择:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读