tags:

views:

76

answers:

4

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.

+3  A: 
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.

anon
thanks, that was my typo. But it still does not work after i added the ;.I will try the answer of 'Randolpho' posted below.
lukmac
+3  A: 

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.

gruszczy
+4  A: 

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**
}
Randolpho
Yeah, how awesome, if you could just code like in `python`, right after class ;-)
gruszczy
Thank you for enlightening me!
lukmac
@Mark B: I may be missing something here, but I think that was supposed to be a joke.
Randolpho
Those C++ coders have no sense of humor :-P
gruszczy
I don't think the assignment of `m[1] = tmp;` is part of his problem. I have declared `static unsigned char montbl[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };` outside of function before. Why would the `m[1] = tmp;` assignment fail outside of a function? Technically you should be able to perform assignment outside of a function.
0A0D
@0A0D: `montbl` is the definition of a global variable (plus its initialization). Definitions are allowed at namespace scope. `m[1]=tmp;` is an assignment, and such statements are not allowed at namespace scope.
sbi
+2  A: 

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.

David Thornley