【发布时间】:2015-11-05 03:15:56
【问题描述】:
在 Map.h 文件的以下代码中未识别国家或大陆的问题
地图.h:
#ifndef MAP_H
#define MAP_H
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
//#include "Country.h"
using namespace std;
class Map{
private:
vector<Country> countries;
vector<Continent> continents;
vector<vector<int> > adjacents;
string author;
string image;
string wrap;
string scroll;
string warn;
public:
Map(ifstream);
Map();
void save();
void setAdjacent(Country&, Country&);
void placeWithin(Continent&, Country&);
int numOfCountries();
int numOfContinents();
bool verify();
bool isAdjacent(Country*, Country&);
bool hasAdjacent(Country*);
bool hasCountry(Continent&);
};
#endif
无论是否添加注释掉的包含,我仍然得到错误。
这是另外两个文件
大陆.h:
#ifndef CONTINENT_H
#define CONTINENT_H
#include "Map.h"
using namespace std;
class Continent
{
private:
string name;
int bonus;
vector<Country> countries;
public:
void addCountry(Country&);
int getOwner();
int getBonus();
int getSize();
string getName();
bool hasCountry(Country&);
};
#endif
国家.h:
#ifndef COUNTRY_H
#define COUNTRY_H
#include "Continent.h"
using namespace std;
class Country{
private:
static int nextCountryNumber;
int countryNumber;
Map map;
string name;
int x, y;
Continent continent;
vector<Country> adjacents;
public:
Country(string, int, int, Continent&, Map&);
string getName();
int getX();
int getY();
Continent getContinent();
bool isAdjacentTo(Country&);
bool hasAdjacent();
void addAdjacent(Country&);
string toString();
};
#endif
我认为它是某种循环引用,但我找不到这种情况......
【问题讨论】:
-
为什么要在 Continent.h 中包含 Map.h?
-
@XiaotianPei,因为
Map在Country中使用,Country.h包含Continent.h,其中包含Map.h -
通常你在标题中使用前向声明而不是
include。否则你可能会遇到循环依赖。另外,具体是什么错误?可以贴在这里吗? -
不是您的主要问题,但
Map(std::ifstream);可能会给您带来麻烦。在 C++11 之前不允许按值传递流,并且一些主流编译器实现该功能的速度很慢。Map(std::ifstream &)会更常见。