我尝试搜索解决方案但找不到。
所以我有一个头文件items.h
:
#ifndef ITEMS_H
#define ITEMS_H
#include <vector>
using std::vector;
int create_item();
class itemClass
{
public:
short int xTile;
short int yTile;
bool playerHas;
short int category;
short int weaponCategory;
}extern vector<itemClass> items;
#endif
然后我items.cpp
我尝试在create_item()
中使用此向量:
#include "stdafx.h"
#include "SDL.h"
#include "items.h"
#include <vector>
using namespace std;
vector<itemClass> items;
int index = 0;
int create_item()
{
//select category for the created item
short int itemCategory = rand() % 3;
switch(itemCategory)
{
case WEAPON:
//increase weapons list by one
items.resize(items.size() + 1);
index = items.size();
//set appropriate item category
items.at(index).category = itemCategory;
items.at(index).weaponCategory = rand() % 9;
break;
}
我遗漏了一些非重要的部分。无论如何,只要我不在标题中将向量声明为extern,这个代码就可以正常工作,但只需将其保持为本地。当我尝试这样做时为什么会导致错误?
编辑:对不起,我忘了包含错误:1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(12):错误 C2371:'items':重新定义;不同的基本类型
1&GT;
c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.h(53):见 声明'项目'1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(36):错误 C2228:'.resize'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(36):错误 C2228:'。size'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(38):错误 C2228:'。size'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(41):错误 C2228:'。at'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(41):错误 C2228:'.category'的左边必须有class / struct / union
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(43):错误 C2228:'。at'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(43):错误 C2228:'.weaponCategory'的左边必须有class / struct / union
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(46):错误 C2228:'。at'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(46):错误 C2228:'.weaponCategory'的左边必须有class / struct / union
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(47):错误 C2228:'。at'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(47):错误 C2228:'.weaponCategory'的左边必须有class / struct / union
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(48):错误 C2228:'。at'的左边必须有class / struct / union
1&GT; type是'int'
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(48):错误 C2228:'.weaponCategory'的左边必须有class / struct / union
1&gt; c:\ users \ aske \ documents \ c ++ \ roguelike \ roguelike \ items.cpp(50):错误 C2228:'。at'的左边必须有class / struct / union
答案 0 :(得分:1)
在;
文件中extern
之前需要items.h
,然后它应该编译 - 但你真的不需要在.h文件中定义项目向量。