非常简单的代码位于同一个文件'foo.h'中:
class Xface
{
public:
uint32_t m_tick;
Xface(uint32_t tk)
{
m_tick=tk;
}
}
std::map<uint32_t, Xface*> m;
Xface* tmp;
tmp = new Xface(100); **//Error**
m[1] = tmp; **//Error**
tmp = new Xface(200); **//Error**
m[2] = tmp; **//Error**
Run Code Online (Sandbox Code Playgroud)
错误是 错误: 每个赋值的'='标记之前的构造函数,析构函数或类型转换.
C++不是脚本语言.您可以声明可执行代码块范围之外的项目,但不能进行任何处理.尝试将错误代码移动到这样的函数中:
int main()
{
std::map<uint32_t, Xface*> m;
Xface* tmp;
tmp = new Xface(100); **//Error**
m[1] = tmp; **//Error**
tmp = new Xface(200); **//Error**
m[2] = tmp; **//Error**
}
Run Code Online (Sandbox Code Playgroud)