Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

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

#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.2incmakewinincmake.exe" --build C:UsersAdministratorCLionProjectsestcmake-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: CMakeFilesest.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]: *** [CMakeFilesest.diruild.make:85: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFilesMakefile2:72: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFilesMakefile2:84: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

CDerivedA的构造函数未实现。

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...