C++:在头文件中声明外部向量容器会导致错误
C++: declaring an extern vector container in header file causes errors
我尝试搜索解决方案,但找不到。
所以我有一个头文件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> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(12):错误C2371:"项目":重新定义;不同的基本类型
1>
c: \users\aske\documents\c++\roguelike\roguellike\items.h(53):请参阅申报"项目"1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(36):错误C2228:".resize"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(36):错误C2228:".size"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(38):错误C2228:".size"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(41):错误C2228:".at"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(41):错误C2228:".classification"的左侧必须具有类/结构/联合
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(43):错误C2228:".at"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(43):错误C2228:".weaponCategory"的左侧必须具有类/结构/联合
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(46):错误C2228:".at"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(46):错误C2228:".weaponCategory"的左侧必须具有类/结构/联合
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(47):错误C2228:".at"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(47):错误C2228:".weaponCategory"的左侧必须具有类/结构/联合
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(48):错误C2228:".at"的左侧必须具有类/结构/联合
1> 类型为"int"
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(48):错误C2228:".weaponCategory"的左侧必须具有类/结构/联合
1> c:\users\aske\documents\c++\roguelike\roguellike\items.cpp(50):错误C2228:".at"的左侧必须具有类/结构/联合
在items.h
文件中,在extern
之前需要一个;
,然后它应该进行编译,但实际上不需要在.h文件中定义项向量。
- C++中函数的向量返回类型引发错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 使用全局声明的向量时,C++双重释放错误/损坏
- 代码使用向量成功运行,但使用数组显示错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么我在向量初始化时收到此错误?
- 在C++中比较来自向量的字符串时出现分段错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 为什么在向量上使用 std::find() 时会出现错误?
- 分割错误:向量中的擦除功能
- 编译错误向量排序和联合
- SIGBART 错误向量<列表<myClass>>
- 编译错误 ::向量 ::迭代器
- 为什么 std::copy 抛出错误向量迭代器 + 偏移量超出范围并且无法复制
- C++STL set_union获取错误向量迭代器+偏移量超出范围
- C++错误向量无法正确输出