[英]Visual Studio 2010 C++ not including files that are in #include
[英]#include files not including (c++)
我在使用C ++中的#include文件时遇到问题。 当我尝试编译以下代码时
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#include "location.h"
#include "Thing.h"
#include "Container.h"
#include <string>
using namespace std;
class Tile //VIRTUAL CLASS
{
protected:
Container onTile; //holds objects that are on the tile
Location * loc; //location
Tile* N; //links to Tiles of that direction (null if nothing)
Tile* E;
Tile* S;
Tile* W;
public:
//no constructor because superclass?
//loc can't move
//actions
bool placeOnTile(Thing * i){return onTile.addItem(i);}
//put a thing on the tile (on the floor)
Thing* takeFrmTile(int i){return onTile.movItem(i);}
//take a thing from the tile (from the floor)
Thing* access(int i) {return onTile.getItem(i);}
//gets an item, but doesn't move it (for like, flipping switches)
//direction setters/getters
void setLoc(Location* i){loc = i;}
void setN(Tile* i){N = i;}
void setE(Tile* i){E = i;}
void setS(Tile* i){S = i;}
void setW(Tile* i){W = i;}
Location* getLoc(){return loc;}
Tile* getN(){return N;}
Tile* getE(){return E;}
Tile* getS(){return S;}
Tile* getW(){return W;}
//displays
void dispOnTile(){onTile.allDisplay();}
void dispSingle(int i){onTile.singleDisplay(i);}
};
我收到未定义“容器”和“事物”的错误消息。 为什么是这样? #include在我看来就像是正确编码的,并且它们在过去已经工作过。 我认为这可能是#include的文件未正确地用括号括起来或未使用相同的名称空间的问题,但它们正确地以(;;结束),并且它们使用的是标准名称空间。 这是怎么回事? 我找不到答案,我知道这一定很简单。 作为记录,#include的文件如下:
#ifndef CONTAINER_H_INCLUDED
#define CONTAINER_H_INCLUDED
#include "Thing.h"
using namespace std;
class Container
{
private:
Thing ** contents; //array of pointers to Things
int numItems; //count item
int maxSize; //maxSize
public:
//constructor
Container(int i) {contents = new Thing*[i]; numItems = 0; maxSize=i;}
//sets num of items (for set-size bags)
Container() {contents = new Thing*[100]; numItems = 0; maxSize=100;}
//creates array of things
~Container() {delete contents;} //cleanup
//actions
bool addItem(Thing* th); //adds item to bag (really just rereferences the pointer)
bool rmvItem(int i); //removes item in array pos i
Thing* getItem(int i); //returns a pointer to item at array pos i
Thing* movItem(int i); //moves an item (deletes it and returns it)
//construction tools
void setMax(int i){delete contents; contents = new Thing*[i];}
//displays
void allDisplay(); //displays entire contents of container, numerated
void singleDisplay(int i); //displays content item i
};
#endif // CONTAINER_H_INCLUDED
#ifndef LOCATION_H_INCLUDED
#define LOCATION_H_INCLUDED
#include <string>
#include <sstream>
#include "Tile.h"
using namespace std;
class Location //stores xy coordinates of something
{
int x; //0 is NOT on map
int y;
Tile* ti; //Locations contain pointers to tiles
public:
//constructors (mainly for debug)
Location(){x=y=0;} //put object OUT OF MAP
Location(int ix, int iy){x=ix;y=iy;} //put object AT loc on map
//setters
void setX(int ix){x=ix;} //sets x
void setY(int iy){y=iy;} //sets y
void setT(Tile*i){ti=i;} //sets Tile
//getters
int getX() {return x;}
int getY() {return y;}
string getloc() //return location as a string, separated by a comma
{
ostringstream locxy; //create stringstream obj to handle input
locxy << getX() << "," << getY() << ". "; //put x, space, y into stringstream
string locret = locxy.str(); //convert stringstream to string
return locret; //return string
}
};
#endif // LOCATION_H_INCLUDED
#ifndef THING_H_INCLUDED
#define THING_H_INCLUDED
#include <string>
#include "location.h"
using namespace std;
class Thing //superclass that will be the base for objects
{
protected:
Location * loc; //location (in or out of map)
string name; //name
string desc; //description
bool deletable; //deletable (for undestructible items)
bool takeable; //if you can put it in your inv
bool hasInv; //returns true if the item has an inventory
public:
//constructor/destructor (debug only)
Thing() //sets initial values
{loc = new Location(0, 0);
name = "Uninitialized";
deletable = takeable = true;
}
Thing(int ix, int iy) //sets location
{loc = new Location(ix, iy);
name = "Uninitialized";
deletable = takeable = false;}
~Thing() {delete loc;} //deletes allocated data
//getters
Location* getLoc() {return loc;} //returns the location
string getDesc(){return desc;} //returns the description
bool getDel(){return deletable;} //returns deletable status
bool getTake(){return takeable;} //returns takeable status
string getName(){return name;} //returns name
string dispLoc(){return loc->getloc();} //displays location
//setters
void setName(string s){name = s;} //sets name
void setDel(bool b){deletable = b;} //sets deletability
void setDesc(string d) {desc = d;} //sets desc
void setLoc(Location* l) {loc = l;} //sets loc
void setTake(bool b){takeable = b;} //sets takeability
//accessors
};
#endif // THING_H_INCLUDED
我相信这是因为您具有递归依赖性。 也就是说,您的类都相互依赖,这意味着某个类将无法编译,因为要进行编译,将需要声明一个类,但无法进行声明。找到声明,因为它位于头文件中的“ #include”堆栈中,并且由于保护而使“ #ifdef”为空。
举个例子。 要编译Tile
,您需要声明Location
,因此,自然地,您#include "location.h"
。 但是,要编译Location
声明,则需要Tile
声明,因此,您需要#include "Tile.h"
。 但是Tile.h
已经被#include
-ed了,所以没有声明!
解决此类循环依赖关系的方法是使用不完整的类声明。 例如,不要将location.h
包含在Tile.h中,而是编写
class Location;
class Tile
{
Location* loc;
}
只要Location
仅用于声明指针或引用并且不访问Location
类的成员,此方法就起作用。
然后,在您的“ Tile.c”文件中,您可以#include "location.h"
并允许您对Tile
方法的扩展访问Location
成员。
您有一个包含循环
Tile.h
包括
location.h
Thing.h
Container.h
Container.h
包括Thing.h
Thing.h
包含location.h
location.h
包括Tile.h
哎呀。
因此,让我们在.cpp
说,第一个包含在内的是Container.h
。 然后在声明任何内容之前包括Thing.h
。 Thing.h
在声明任何内容之前包含location.h
。 然后在声明任何内容之前包括Tile.h
在Tile.h
,所有包括防护使递归包括无操作。
因此,在Tile.h
, Thing
和Container
是未知的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.