Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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::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**

The error is error: expected constructor, destructor, or type conversion before '=' token for every assignment.

like image 460
lukmac Avatar asked Dec 04 '22 11:12

lukmac


2 Answers

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<uint32_t, Xface*> m;

    Xface* tmp;

    tmp = new Xface(100);  **//Error**
    m[1] = tmp;  **//Error**

    tmp = new Xface(200);  **//Error**
    m[2] = tmp;  **//Error**
}
like image 99
Randolpho Avatar answered Jan 19 '23 00:01

Randolpho


Your code must be inside some function, you can't just put it in void :-) Try running the same code in main and see, what happens.

like image 30
gruszczy Avatar answered Jan 19 '23 00:01

gruszczy