C++ 错误 C2039:“向量”:不是“标准”的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39765112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR C2039: 'vector': is not a member of 'std'
提问by Jelmer
I am new to C++ and I am trying to make a little dungeon crawler game. Currently I have multiple vectors declared in my header files but they seem to give multiple errors. I have tried searching for this problem on StackOverflow but the answers don't really seem to work.
我是 C++ 的新手,我正在尝试制作一个小地牢爬虫游戏。目前我在头文件中声明了多个向量,但它们似乎给出了多个错误。我曾尝试在 StackOverflow 上搜索这个问题,但答案似乎并不奏效。
Here is one of my header files: (Hero.h)
这是我的头文件之一:(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:
};
Here is my .cpp file: (Hero.cpp)
这是我的 .cpp 文件:(Hero.cpp)
#include "stdafx.h"
#include <vector>
#include "Hero.h"
#include "Item.h"
Hero::Hero() {
}
void Hero::levelUp()
{
};
Like I said I am new to C++ so there might be a lot more wrong with my code than I know. This is just a test.
就像我说的,我是 C++ 的新手,所以我的代码可能有比我知道的更多的错误。这只是一个测试。
Below are the errors that are shown in the Error list of Visual Studio 2015:
下面是 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
回答by
Include <vector>
in your Hero.hheader and consider removing it from your Hero.cppfile as mentioned in the comments below.
包括<vector>
在您的Hero.h标头中,并考虑将其从您的Hero.cpp文件中删除,如下面的评论中所述。
回答by Bathsheba
std::vector<Item> items = std::vector<Item>();
declares a complete type.
std::vector<Item> items = std::vector<Item>();
声明一个完整的类型。
Therefore the compiler needs to know the declarationof std::vector
at that point (amongst other things, it's required to establish the compile-time evaluable constant sizeof Hero
). The solution is to #include <vector>
in the header hero.h
, notthe source file.
因此,编译器需要知道声明的std::vector
在这一点上(除其他事项外,它必须建立在编译时评估不变sizeof Hero
)。解决方案是#include <vector>
在 header 中hero.h
,而不是在源文件中。