C++ - 错误:'list'没有命名类型(将对象列为类中的成员变量)

C++ - Error: 'list' does not name a type (list object as member variable in class)

本文关键字:对象 变量 成员 list 错误 类型 C++      更新时间:2023-10-16

我一直遇到"'xxx'不命名类型"错误,我之前读过的大多数帖子都提到此错误发生在一些依赖项问题中。 但是,我似乎找不到我的。 这是我得到的:

GameLib.h

#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_ */

玩家组.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_ */

玩家组.cpp

#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);
}
.....

我在 PlayerGroup 类中的两个列表成员变量上都收到此错误:

..srcPlayerGroup.h:13:2: error: 'list' in namespace 'std' does not name a type
..srcPlayerGroup.h:14:2: error: 'list' in namespace 'std' does not name a type

提前感谢所有帮助!

我相信你需要在你的PlayerGroup.h文件中#include <list>,因为它用在那个文件中。