【问题标题】:abstract class program providing LNK1169 error提供 LNK1169 错误的抽象类程序
【发布时间】:2018-12-01 20:30:54
【问题描述】:

我正在开发一个使用抽象类的 cpp 程序。显然编译器不接受我声明了抽象类和派生类的事实。我有不同的文件(.cpp 和 .h)。错误如下:

1>IGeomObj.obj : error LNK2005: "public: virtual void __thiscall IGeomObj::circumference(void)" (?circumference@IGeomObj@@UAEXXZ) 已在 GeoRect.obj 中定义

1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall GeoRect::circumference(void)" (?circumference@GeoRect@@UAEXXZ)

1>IGeomObj.exe:致命错误 LNK1120:1 个未解决的外部 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

这里是代码: 主要()

 # include <iostream>
 # include "IGeomObj.h"
 # include "GeoCircle.h"
 # include "GeoEllipse.h"
 # include "GeoRect.h"
 # include "GeoSquare.h"
 # include "GeoTriangle.h"

 using namespace std;

 int main()
 {
/*GeoSquare *R;
GeoTriangle *R;
GeoRect *R;
GeoRect *R;*/
int opt;
do{
    cout<<endl<<"Menu:"<<endl<<endl<<"0.    Exit"<<endl<<"1.    New rectangle"
        <<endl<<"2. New Square"<<endl<<"3.  New Triangle"<<endl<<"4.    New        Circle"<<endl
        <<"5.   New Ellipse"<<endl;
        cin>>opt;
    switch(opt)
    {
    case 0:
        break;
    case 1:
        GeoRect *R=new GeoRect;
        cout<<endl<<"Height of the rectangle: ";
        cin>>R->h;
        cout<<endl<<"Width of the rectangle: ";
        cin>>R->b;
        R->output();
        break;

    };
}while(opt!=0);

system("pause");
return 0;
}

IGeomObj.h

 #pragma once
 #ifndef IGEOMOBJ_H
 #define IGEOMOBJ_H

 #include <iomanip>
 #include <fstream>

 class IGeomObj{

 public:

float b,h,r,f,u;
virtual void output()=0;
virtual void area()=0;
virtual void circumference()=0;
};
#endif

IGeomObj.cpp

 #include "IGeomObj.h"
 #include <iostream>
 using namespace std;

 void IGeomObj::output(){};
 void IGeomObj::area(){};
 void IGeomObj::circumference(){};

GeoRect.h

 #pragma once
 #ifndef GEORECT_H
 #define GEORECT_H

 #include <iomanip>
 #include <fstream>
 #include "IGeomObj.h"

 class GeoRect:public IGeomObj
 {
 public:
    virtual void output();
    virtual void area();
    virtual void circumference();
 };

 #endif

GeoRect.cpp

  #include "GeoRect.h"
  #include <iostream>
  using namespace std;

  void GeoRect::output()
  {  
    cout<<"Rectangle Area: "<<this->f<<" Circumference: "<<this->u;
  };

  void GeoRect::area()
  {
  this->f=this->h*this->b;
  };

  void IGeomObj::circumference()
   {
     this->u=2*this->h+2*this->b;
   };

【问题讨论】:

    标签: c++ virtual abstract void


    【解决方案1】:

    您的 GeoRect.cpp 文件实现了IGeomObj::circumference(),这是合法的,因为您可以提供纯虚函数的实现。

    但是,如果您还没有实现 GeoRect::circumference(),这还不够,因为基类函数被声明为纯虚函数。

    【讨论】:

      【解决方案2】:

      在 GeoRect.cpp 中有一个 IGeomObj::circumference 的定义。

      void IGeomObj::circumference()
      {
         this->u=2*this->h+2*this->b;
      };
      

      但这应该是 GeoRect 的定义。

      void GeoRect::circumference()
      {
         this->u=2*this->h+2*this->b;
      };
      

      也没有必要为每个对成员的引用添加this-&gt;

      void GeoRect::circumference()
      {
         u=2*h+2*b;
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多