ERROR C2039:'vector':不是'std'的成员

Jel*_*mer 10 c++ stdvector visual-studio c++11

我是C++的新手,我正在尝试制作一个小地牢爬虫游戏.目前我在头文件中声明了多个向量,但它们似乎给出了多个错误.我试过在StackOverflow上搜索这个问题,但答案似乎并没有起作用.

这是我的一个头文件:(Hero.h)

#pragma once

class Hero {
public:
    Hero();
    std::string name;
    int experience;
    int neededExperience;
    int health;
    int strength;
    int level;
    int speed;
    std::vector<Item> items = std::vector<Item>();
    void levelUp();
private:
};
Run Code Online (Sandbox Code Playgroud)

这是我的.cpp文件:(Hero.cpp)

#include "stdafx.h"
#include <vector>
#include "Hero.h"
#include "Item.h"

Hero::Hero() {

}
void Hero::levelUp()
{

};
Run Code Online (Sandbox Code Playgroud)

就像我说我是C++的新手,所以我的代码可能比我知道的更多错.这只是一个考验.

以下是Visual Studio 2015的错误列表中显示的错误:

Error   C2039   'vector': is not a member of 'std'  CPPAssessment   hero.h  13  
Error   C2143   syntax error: missing ';' before '<'    CPPAssessment   hero.h  13  
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    CPPAssessment   hero.h  13  
Error   C2238   unexpected token(s) preceding ';'   hero.h  13  
Run Code Online (Sandbox Code Playgroud)

小智 16

包含<vector>在您的Hero.h标头中,并考虑将其从Hero.cpp文件中删除,如下面的评论中所述.

  • 不,不要删除它,它可能*正确. (3认同)

Bat*_*eba 5

std::vector<Item> items = std::vector<Item>();声明一个完整的类型.

因此,编译器需要知道声明的std::vector在这一点上(除其他事项外,它必须建立在编译时评估不变sizeof Hero).解决方案是#include <vector>在标头中hero.h,而不是源文件.