请注意,这里是cpp的新手,可能是愚蠢的。
我在实现虚函数时试图返回一个静态属性。这个问题是一个链接器错误,指出该函数没有实现。我迷失了这个问题。
我能够使用以下精简代码重现该错误:
#include <iostream>
#include <map>
class Abstract1 {
public:
virtual char* getFoo();
};
class Base: public Abstract1 {
public:
char* getFoo() {
return Base::mapper[1];
}
static std::map<int,char*> mapper;
};
std::map<int, char*> Base::mapper;
int main(int argc, const char * argv[]) {
Base::mapper[0] = "Hello!\n";
Base::mapper[1] = "Goodbye!\n";
Base* hello = new Base();
// insert code here...
std::cout << hello->getFoo() << "\n";
return 0;
}
产生以下链接器错误:
Undefined symbols for architecture x86_64:
"typeinfo for Abstract1", referenced from:
typeinfo for Base in main.o
"vtable for Abstract1", referenced from:
Abstract1::Abstract1() in main.o