链接此代码时:
#include <map>
using std::map;
#include <string>
using std::string;
class C {
public:
static void dump() {
for (const auto& e : data) {
string(e.first);
}
}
private:
static map<string,map<string,string>> data;
};
int main() {
C::dump();
}
...我得到这个错误:
/tmp/cc4W2iNa.o: In function `C::dump()':
test.cpp:(.text._ZN1C4dumpEv[_ZN1C4dumpEv]+0x9): undefined reference to `C::data'
collect2: error: ld returned 1 exit status
...来自g ++(GCC)4.9.1。我做错什么了吗?
您已声明C::data
,但未定义它。在类外添加一个定义:
map<string,map<string,string>> C::data;
在一个较大的程序中,该程序具有多个源文件,必须将此文件仅放入一个源文件中才能满足“一个定义规则”。而类定义(包括的声明data
)可能会放在标头中,以在任何需要的地方可用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句