错误C2039:' vector':不是' std'的成员。

时间:2016-09-29 08:25:06

标签: c++ visual-studio c++11 stdvector

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

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

`SELECT COUNT(*) , books_bookid
 FROM bookgenre_has_books
 GROUP BY books_bookid
 having count(*) > 10`

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

#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:
};

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

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

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

Hero::Hero() {

}
void Hero::levelUp()
{

};

3 个答案:

答案 0 :(得分:14)

Hero.h 标题中添加<vector>,并考虑将其从 Hero.cpp 文件中删除,如下面的评论中所述。

答案 1 :(得分:5)

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

因此,编译器需要知道std::vector声明(除此之外,还需要建立编译时可评估常量sizeof Hero)。解决方案是标题#include <vector>中的hero.h源文件。

答案 2 :(得分:0)

//.h
template<class _Ty>
class std::allocator;
template<class _Ty, class _Alloc = std::allocator<_Ty> >
class std::vector;

//this line in .cpp file. Put it before custom header files.
#include<vector>

在2017年进行测试。 这样,您的自定义头文件中就不会包含头文件。 但不确定为什么堆栈的保存方法不起作用。

有关更多信息,请https://github.com/YagaoDirac/snippet-set-for-cpp。还有一个不和谐的链接。