lukmac
lukmac

Reputation: 191

c++ compiling "error: 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**

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

Upvotes: 4

Views: 31889

Answers (4)

David Thornley
David Thornley

Reputation: 57036

You have no default constructor. You need to have a constructor that doesn't need any arguments. Right now, you've got a constructor that needs a uint32_t, so you can't new an array of them. Not to mention, as Neil pointed out, the missing semicolon, and gruszczy's observation that executable code needs to be in a function.

Upvotes: 2

Randolpho
Randolpho

Reputation: 56391

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

Upvotes: 8

gruszczy
gruszczy

Reputation: 42168

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.

Upvotes: 4

anon
anon

Reputation:

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

You are missing a semicolon at the end of the class definition.

Upvotes: 3

Related Questions