所以,我尝试使用Visual Studio 2010在C ++中制作基于文本的游戏。以下是我认为相关的一些代码块。如果您需要,请不要犹豫,问我。
我试图为名为地方的游戏创建一个类。我创造了一个地方,它有另一个地方"到它的北,南,东和西。我现在真的很困惑。我是这个东西的菜鸟。我可能只是在寻找一些东西。
//places.h------------------------
#include "place.h"
//Nowhere place
string nowheredescr = "A strange hole to nowhere";
place nowhere(&nowheredescr, &nowhere, &nowhere, &nowhere, &nowhere); //Error occurs here
//
//place.h------------------------
#ifndef place_h
#define place_h
#include "classes.h"
class place
{
public:
place(string *Sdescription, place *Snorth, place *Ssouth, place *Swest, place *Seast);
~place(void);
private:
string *description;
place *north;
place *south;
place *east;
place *west;
};
#endif
//place.cpp-------------------
#include "place.h"
#include <iostream>
place::place(string *Sdescription, place *Snorth, place *Ssouth, place *Swest, place *Seast)
{
description = Sdescription;
north = Snorth;
south = Ssouth;
west = Swest;
east = Seast;
}
place::~place(void)
{
}
答案 0 :(得分:2)
以下语法将解决错误
place nowhere = place(&nowheredescr, &nowhere, &nowhere, &nowhere, &nowhere);
在C ++ 03标准3.3.1 / 1
中对此进行了解释名称的声明点在完成后立即生效 声明者(第8条)和初始化者(如果有的话)
在OP示例中,place nowhere(.....)
表示声明符,因此用作构造函数参数的nowhere
被视为未声明。
在我的示例中,place nowhere
是声明符,place(.....)
是初始化程序,因此nowhere
在此时被声明。