我在下面的代码中遇到以下错误。
expected constructor, destructor, or type conversion before '=' token
--
#include <string>
#include <map>
class Foo {
};
std::map<std::string, Foo> map;
map["bar"] = Foo();
int main()
{
return 0;
}
最佳答案
map["bar"] = Foo(); // This line is a statement not a declaration.
// You have to put it in main, or any execution context
在 C++0x 成为主流之前,我建议使用 boost
。填充 map
变得轻而易举。这是一个例子:
std::map<std::string, Foo> mymap;
...
int main()
{
insert(mymap)
("First", Foo(...))
("Second", Foo(...))
("Third", Foo(...));
...
}
关于c++ - 在全局级别实例化 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1627866/