Sign in to follow this  
reapz

C2059 : Syntax Error 'constant'

Recommended Posts

I am trying to initialise instances as members of a class, but am having an error I'm sure how to fix. Here's the Board class header: #ifndef _BOARD_H_ #define _BOARD_H_ #include "SexyAppFramework/Widget.h" #include "Graph.h" #include "Pacman.h" #include "Resources.h" #include "TileBackground.h" class Node; namespace Sexy { class GameApp; class Graphics; class Board : public Widget { private: GameApp* mApp; Graph theGraph; TileBackground theTileBackground(32, 22); // Problematic Line Pacman thePacman(0, PACMAN, 0.0f, 0.0f, 0.0f, 0.0f, 32, 32); // Problematic Line void buildGraph(); public: Board(GameApp *theApp); virtual ~Board(); virtual void Update(); virtual void Draw(Graphics *g); }; } #endif Heres the TileBackground header: #ifndef _TILEBACKGROUND_H_ #define _TILEBACKGROUND_H_ #include <list> #include <vector> namespace Sexy { class Graphics; } class Tile; class TileBackground { private: int mX, mY; int mTileCountX, mTileCountY; std::vector<Tile*> mTiles; public: TileBackground(int tileCountX, int tileCountY); void draw(Sexy::Graphics *g); void buildTileBG(std::list<int> theList); bool isTileJunction(Tile *theTile, bool &up, bool &down, bool &left, bool &right); void removeSection(const int §ion); std::vector<Tile*>::iterator firstTile() { return mTiles.begin(); } std::vector<Tile*>::iterator lastTile() { return mTiles.end(); } }; #endif Here's my error output: 1>------ Build started: Project: Pacman, Configuration: Debug Win32 ------ 1>Compiling... 1>GameApp.cpp 1>e:\programming\pacmangame\pacman\barebones\Node.h(29) : warning C4244: '=' : conversion from 'const int' to 'float', possible loss of data 1>e:\programming\pacmangame\pacman\barebones\Board.h(25) : error C2059: syntax error : 'constant' 1>e:\programming\pacmangame\pacman\barebones\Board.h(26) : error C2059: syntax error : 'constant' 1>Board.cpp 1>e:\programming\pacmangame\pacman\barebones\Node.h(29) : warning C4244: '=' : conversion from 'const int' to 'float', possible loss of data 1>e:\programming\pacmangame\pacman\barebones\Board.h(25) : error C2059: syntax error : 'constant' 1>e:\programming\pacmangame\pacman\barebones\Board.h(26) : error C2059: syntax error : 'constant' 1>.\Board.cpp(74) : error C2228: left of '.buildTileBG' must have class/struct/union 1>.\Board.cpp(80) : error C2228: left of '.setCurrentNode' must have class/struct/union 1>.\Board.cpp(137) : error C2228: left of '.draw' must have class/struct/union 1>.\Board.cpp(169) : error C2228: left of '.firstTile' must have class/struct/union 1>.\Board.cpp(170) : error C2228: left of '.lastTile' must have class/struct/union 1>.\Board.cpp(180) : error C2228: left of '.isTileJunction' must have class/struct/union 1>.\Board.cpp(201) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data 1>.\Board.cpp(212) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data 1>Generating Code... 1>Build log was saved at "file://e:\Programming\PacmanGame\Pacman\BareBones\Debug\BuildLog.htm" 1>Pacman - 10 error(s), 4 warning(s) ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========== The syntax error constant is what i'm worried about because it seems to all stem from that. I am a bit iffy in having Instances as member variables, how does one go about initialising them if I want them to be initialised with variables. I will be changing to pointers in the mean time but that kind of defeats the purpose as pointers are not needed and for clarity...

Share this post


Link to post
Share on other sites
You can't initialise variables like that, you need to do it from the constructor.

Those two lines should be:
TileBackground theTileBackground;
Pacman thePacman;


And then your constructor will need to be (in the .cpp file):

Board::Board(GameApp *theApp) :
theTileBackground(32, 22),
thePacman(0, PACMAN, 0.0f, 0.0f, 0.0f, 0.0f, 32, 32)
{
}


(Plus any other initialisation you're doing).

Share this post


Link to post
Share on other sites
You can't initialize members where you declare them, you have to do it in the constructor initializer list.

class Example
{
SomeType x;
public:
Example(int a, int b) : x(a, b) {}
}

Share this post


Link to post
Share on other sites
You can't use inline initialisation of member variables in C++. Instead, you declare the instance variable as normal, and then initialise them in the initialiser list of the constructor:
class Board : public Widget
{
private:
TileBackground theTileBackground;
Pacman thePacman;

public:
Board(GameApp *theApp) : theTileBackground(32, 22), thePacman(0, PACMAN, 0.0f, 0.0f, 0.0f, 0.0f, 32, 32) { }
};

If you also need to initialise a base-class (in this case, Widget) in the constructor's initialiser list, you need to do so before initialising any member variables.

Share this post


Link to post
Share on other sites
Thankyou very much for the quick response guys, I knew it was something simple, I just got into the habit of using pointers so I hadn't come accross this before!!!

I have a similarish problem, so I'll start a new thread shortly. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
If you also need to initialise a base-class (in this case, Widget) in the constructor's initialiser list, you need to do so before initialising any member variables.

You don't NEED to (the order in the initializer list does not matter), though good compilers will warn you.

struct Base
{
int x;
Base(int x) : x(x) {}
};

struct Derived : Base
{
int y;
Derived(int x, int y) : y(y), Base(x) {} // Base will still be initialized before y
};

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by swiftcoder
If you also need to initialise a base-class (in this case, Widget) in the constructor's initialiser list, you need to do so before initialising any member variables.
You don't NEED to (the order in the initializer list does not matter), though good compilers will warn you.
My point was more that you can't use derived member variables to initialise a base class.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this