c ++编译"错误:'='标记之前的预期构造函数,析构函数或类型转换"

luk*_*mac 4 c++

非常简单的代码位于同一个文件'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)

错误是 错误: 每个赋值的'='标记之前的构造函数,析构函数或类型转换.

Ran*_*pho 8

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)

  • 是的,多么棒,如果你可以在课堂上像`python`那样编码;-) (2认同)
  • 那些C++程序员没有幽默感:-P (2认同)