I am trying to compile the following code:
#include <iostream>
class X{
public:
virtual void func();
};
class Y : public X{
public:
virtual void func(){
std::cout << "y" << std::endl;
}
};
int main(){
Y* y = new Y();
y->func();
return 0;
}
But building fails (on Xcode - C++11) with the following messages:
Undefined symbols for architecture x86_64:
"typeinfo for X", referenced from:
typeinfo for Y in c.o
"vtable for X", referenced from:
X::() in c.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, as soon as I add an implementation for func in X, it builds successfully. I am pretty sure, that virtual method is optional to be implemented in the superclass but I don't understand why is this happening. Also, if comment the code in main(), it builds successfully. I am assuming that the problem is calling the func() in main, but Xcode doesn't list it as runtime error, it only says build-time error.
Aucun commentaire:
Enregistrer un commentaire