C++ C++;-错误:';列表';不命名类型(将对象列为类中的成员变量)
我经常遇到“'xxx'不命名类型”错误,我以前读过的大多数帖子都提到这个错误是由于一些依赖性问题引起的。然而,我似乎找不到我的。以下是我得到的: GameLib.h PlayerGroup.hC++ C++;-错误:';列表';不命名类型(将对象列为类中的成员变量),c++,class,stdlist,C++,Class,Stdlist,我经常遇到“'xxx'不命名类型”错误,我以前读过的大多数帖子都提到这个错误是由于一些依赖性问题引起的。然而,我似乎找不到我的。以下是我得到的: GameLib.h PlayerGroup.h 提前谢谢你的帮助 我认为您需要在PlayerGroup.h文件中包含,因为它在该文件中使用。您需要在PlayerGroup.h中包含。在使用std::list之前,您应该包含。您是否希望任何包含该标头的文件在包含标头之前为您随机包含该标头?我希望不是。std::string也一样。我想在.cpp中包含必要
提前谢谢你的帮助 我认为您需要在PlayerGroup.h文件中包含,因为它在该文件中使用。您需要在
PlayerGroup.h
中包含。在使用std::list
之前,您应该包含
。您是否希望任何包含该标头的文件在包含标头之前为您随机包含该标头?我希望不是。std::string也一样。我想在.cpp中包含必要的文件也会包含在头文件中。那么,这个假设是错误的吗?在头文件中包含文件有什么缺点或副作用吗?(我从来不知道可以在头文件中包含文件)不,不应该有任何缺点。就像在.cpp文件中使用#include一样,这样可以将某个文件链接到另一个头文件以使用其实现。在这种情况下,只在.cpp文件中使用#include应该只让.cpp文件使用它的方法。@JoeG实际上有一些缺点。如果您有一个大型项目,并且未经考虑就将头包含到头中,则可能会产生许多不必要的依赖项,并且可能会不必要地增加编译时间。不过,若您在类中存储了一些对象(不是指向对象的指针),那个么除了使用包含常用头的预编译头之外,您将无能为力。
#ifndef GAMELIB_H_
#define GAMELIB_H_
//Structures
struct player_t {
std::string name;
int MMR;
};
//Prototypes
void* queueUpPlayer(void*);
int randomMMR();
std::string randomName();
#endif /* GAMELIB_H_ */
#ifndef GROUP_H_
#define GROUP_H_
class playerGroup {
private:
std::list<player_t> players;
std::list<player_t>::iterator it;
const int totalSize = 10;
public:
//Constructor
playerGroup();
//Destructor
~playerGroup();
//Add
void add(const player_t p);
....
};
#endif /* GROUP_H_ */
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <list>
#include "GameLib.h"
#include "playerGroup.h"
using namespace std;
playerGroup::playerGroup() {}
playerGroup::~playerGroup() {}
void playerGroup::add(const player_t p) {
if(players.size() >= totalSize) exit(1);
players.push_back(p);
}
.....
..\src\PlayerGroup.h:13:2: error: 'list' in namespace 'std' does not name a type
..\src\PlayerGroup.h:14:2: error: 'list' in namespace 'std' does not name a type