trying to initiate a class with a constructor that takes an argument
class tok开发者_StackOverflow中文版en
{
private:
char m_chIcon; //actual ascii char that shows up for this token
location m_cPlayerLocation; // every token has a location
token() {}
public:
token(char icon) : m_chIcon(icon) {}
};
class board
{
private:
token m_cPlayer('@');
};
I have tried with and without initialization lists. From what I have looked into so far, it seems the compiler thinks I am trying to declare a function with the return type token. I also tried using a name other than token to see is that was a conflict.
also I am getting the error on this line:
token m_cPlayer('@');
Error: expected type specifier
and then any other reference further down the line of m_cPlayer
Error: expression must have class type
I have removed other surrounding code from what I posted that I don't believe is causing errors.
Member variables in C++03 can only be initialized inside a function (e.g. from a constructor):
class board {
private:
token m_cPlayer;
public:
board() : m_cPlayer('@') {}
};
1) You are trying to create static field. Then you should write
class board
{
private:
static token m_cPlayer;
};
// Then in **ONE** source file add.
token board::m_cPlayer('@');
2) You are trying to create default value. Then you should write
class board
{
private:
token m_cPlayer;
public:
board() : m_cPlayer('@') {}
};
精彩评论