c++ compiling “error: expected constructor, destructor, or type conversion before '=' token ”

后端 未结 4 438
暖寄归人
暖寄归人 2021-01-19 03:55

Very simple codes located in the same file \'foo.h\':

class Xface
{
  public:
    uint32_t m_tick;
    Xface(uint32_t tk)
    {
      m_tick=tk;
    }
}

std         


        
4条回答
  •  野的像风
    2021-01-19 04:53

    C++ is not a scripting language. You can declare items outside the bounds of an executable block of code, but you cannot do any processing. Try moving the erroring code into a function like this:

    int main()
    {
        std::map m;
    
        Xface* tmp;
    
        tmp = new Xface(100);  **//Error**
        m[1] = tmp;  **//Error**
    
        tmp = new Xface(200);  **//Error**
        m[2] = tmp;  **//Error**
    }
    

提交回复
热议问题