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

Joe*_*Joe 7 c++ class stdlist

我一直在遇到"'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_ */
Run Code Online (Sandbox Code Playgroud)

PlayerGroup.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_ */
Run Code Online (Sandbox Code Playgroud)

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

.....
Run Code Online (Sandbox Code Playgroud)

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

..\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
Run Code Online (Sandbox Code Playgroud)

感谢您提前获得所有帮助!

Ale*_*son 16

我相信您需要#include <list>在您的PlayerGroup.h文件中,因为它在该文件中使用.

  • 不,不应该有任何缺点.就像在.cpp文件中使用#include一样,可以将某个文件与另一个头文件链接以使用其实现.在这种情况下,仅在.cpp文件中使用#include应该只给.cpp文件使用其方法. (2认同)