I have a question about an error I’m getting when trying to compile a card game I am making.
I have a class named Player which takes a const char* as a parameter for it’s constructor.
I am trying to create 4 instances of Player in a struct called GameState however it is giving me this error.
In file included from testfile.cc:5:0:
gamestate.h:22:17: error: expected identifier before string constant
gamestate.h:22:17: error: expected â,â or â...â before string constant
gamestate.h:23:17: error: expected identifier before string constant
gamestate.h:23:17: error: expected â,â or â...â before string constant
gamestate.h:24:17: error: expected identifier before string constant
gamestate.h:24:17: error: expected â,â or â...â before string constant
gamestate.h:25:18: error: expected identifier before string constant
gamestate.h:25:18: error: expected â,â or â...â before string constant
In file included from player.cc:3:0:
gamestate.h:22:17: error: expected identifier before string constant
gamestate.h:22:17: error: expected â,â or â...â before string constant
gamestate.h:23:17: error: expected identifier before string constant
gamestate.h:23:17: error: expected â,â or â...â before string constant
gamestate.h:24:17: error: expected identifier before string constant
gamestate.h:24:17: error: expected â,â or â...â before string constant
gamestate.h:25:18: error: expected identifier before string constant
gamestate.h:25:18: error: expected â,â or â...â before string constant
In file included from game_functions.cc:3:0:
gamestate.h:22:17: error: expected identifier before string constant
gamestate.h:22:17: error: expected â,â or â...â before string constant
gamestate.h:23:17: error: expected identifier before string constant
gamestate.h:23:17: error: expected â,â or â...â before string constant
gamestate.h:24:17: error: expected identifier before string constant
gamestate.h:24:17: error: expected â,â or â...â before string constant
gamestate.h:25:18: error: expected identifier before string constant
gamestate.h:25:18: error: expected â,â or â...â before string constant
The code for GameState is
#ifndef __GAMESTATE_H__
#define __GAMESTATE_H__
#include <gtk/gtk.h>
#include "deck.h"
#include "player.h"
#include "trick.h"
using namespace std;
struct GameState
{
GtkWidget *ai1_hand_image;
GtkWidget *ai2_hand_image;
GtkWidget *ai3_hand_image;
GtkWidget *play_area;
GtkWidget *info_label;
GtkWidget *pass_button;
GtkWidget *play_card_button;
GtkWidget *player_hand;
Player ai1( "ai1" );
Player ai2( "ai2" );
Player ai3( "ai3" );
Player user( "user" );
Deck deck();
Trick current_trick;
int trick_num;
bool hearts_broken;
};
#endif
The header file for Player is
#ifndef __PLAYER_H__
#define __PLAYER_H__
class GameState;
#include <vector>
#include "card.h"
using namespace std;
class Player
{
public:
Player( const char *_name );
void add_to_hand( Card _card);
void remove_from_hand( Card _card );
bool hand_contains( Card _card );
void set_valid_cards( GameState *game_state );
vector < Card > get_valid_cards();
const char *get_name();
private:
const char *name;
vector < Card > hand;
vector < Card > valid_cards;
};
#endif
However when I make them Pointers in the struct it works fine. Also creating instances of Player outside of GameState also works.
This works:
Player *ai1;
Player *ai2;
Player *ai3;
Player *user;
and this when it is in testfile.cc:
Player user( "user" );
Can anyone tell me why I get those errors when I create them inside GameState.
Thanks alot guys!
You can’t declare objects inside a class (or struct) definition. You have to do it in a function.
Either use a special function that initializes the objects, or better yet have a constructor do it (in C++ a struct is just a special class where all members are public by default):