我有一个关于我在尝试编制正在制作的纸牌游戏时遇到的错误的问题。 我有一个名为Player的类,它将const char *作为它的构造函数的参数。 我试图在一个名为GameState的结构中创建4个Player实例,但它给了我这个错误。
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
GameState的代码是
#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
Player的头文件是
#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
然而,当我在结构中制作指针时它工作正常。另外,在GameState之外创建Player的实例也可以。
这有效:
Player *ai1;
Player *ai2;
Player *ai3;
Player *user;
,当它在testfile.cc中时:
Player user( "user" );
当我在GameState中创建错误时,有人能告诉我为什么会出现这些错误。 非常感谢你们!
答案 0 :(得分:3)
您无法提供初始化程序
Player ai1( "ai1" );
在类定义中。您可以声明该成员,然后在构造函数中初始化它。像
Player ai1, ai2, ai3, user;
GameState() : ai1("ai1"), ai2("ai2"), ai3("ai3"), user("user") { }
答案 1 :(得分:0)
您不能在类(或结构)定义中声明对象。你必须在一个函数中完成它。
要么使用初始化对象的特殊函数,要么更好地让构造函数执行它(在C ++中,struct只是一个特殊的类,默认情况下所有成员都是公共的):
struct GameState
{
Gamestate()
: ai1( "ai1" ), ai2( "ai2" ), ai3( "ai3" ), user( "user" ), deck()
{ }
// The other fields...
Player ai1;
Player ai2;
Player ai3;
Player ai4;
Player user;
Deck deck;
};