C ++ compilation ": expected constructor, destructor or type conversion before '=' token"

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

Error : expected constructor, destructor, or type conversion before the '=' token for each assignment.

+4
source share
4 answers

C ++ is not a scripting language. You can declare elements outside the executable code block, but you cannot perform any processing. Try moving the error code to 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** } 
+8
source

Your code must be inside some function, you cannot just put it in void :-) Try to run the same code in the main one and see what happens.

+4
source
 class Xface { public: uint32_t m_tick; Xface(uint32_t tk) { m_tick=tk; } } // need a semicolon here 

There is no semicolon at the end of the class definition.

+3
source

You do not have a default constructor. You must have a constructor that does not need any arguments. Right now, you have a constructor that needs uint32_t , so you cannot new array from them. Not to mention the fact that, as Neil pointed out, the missing semicolon and the serious remark that the executable code must be in the function.

+2
source

All Articles