C++代码编译时报错 未知原因,求解

stephen2017
  • 10
#include"iostream"
using namespace std;
class CBase
{
public :
    CBase();
    virtual void Walk (){cout<<"CBase:walk"<<endl;}
    virtual void Jump(){cout<<"CBase:Jump"<<endl;}
    void Run(int speed){cout<<"Cbase:Run:"<<"Speed="<<"Speed="<<speed<<endl;}
};
class CDerivedA:public CBase
{
public:
    CDerivedA();
    void Walk(){cout<<"CDerivedA:Walk"<<endl;}
    void Jump(){cout<<"CDerivedA:Jump"<<endl;}
    void Run(int speed){cout<<"CDerivedA:Run"<<"Speed="<<speed<<endl;}
};
int main() {
    CBase *pTmp1=new CDerivedA;
    pTmp1->Walk();
    pTmp1->Run(20);
    pTmp1->Run(30);
    return 0;
}

报错如下:

"F:\CLion 2018.2.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\Administrator\CLionProjects\test\cmake-build-debug --target test -- -j 2
[ 50%] Linking CXX executable test.exe
f:/load/mingw-17.1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/Administrator/CLionProjects/test/main.cpp:20: undefined reference to `CDerivedA::CDerivedA()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\test.dir\build.make:85: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2
评论
阅读 370
1 个回答
✓ 已被采纳

CDerivedA的构造函数未实现。

C:/Users/Administrator/CLionProjects/test/main.cpp:20: undefined reference to `CDerivedA::CDerivedA()'
撰写回答

登录后参与交流、获取后续更新提醒

宣传栏