简体   繁体   中英

C++ Class Composition error: unrefined reference

Having this annoying error.

I keep getting undefined reference to class::method

This is the code that I'd like to do

student::student(){
conscience* c;
unsigned short choice = rand() % 4;
if (choice == 0){
    c = new betray();
}
if (choice == 1){
    c = new silent();
}
if (choice == 2){
    c = new confused();
}
if (choice == 3){
    c = new experienced();
}

but I keep getting the error.

This work around works nicely but is not in the main classes constructor but passed by arguments

student::student(conscience* c){
    m_Conscience = c;
}

Thanks in advance.

Edit: Exact error: undefined reference to `betray::betray()'

Undefined reference can mean one of the following:

  1. You have a method declaration in a base class, which you call but is never implemented in any derived class.
  2. Or, it is implemented, but you are not linking to it, so the linker can't find it.

I suggest you check if you are linking against betray.o. Check your makefile. If you don't have a makefile, I encourage you to write one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM