I tried searching for solution but couldn't find.
So I have a header file 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
and then I have items.cpp
where I try to use this vector in 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;
}
I left out some non-important parts out. Anyway this code works fine as long as I don't declare the vector as extern in the header, but just keep it local. Why does it cause errors when I try to do this?
EDIT: Sorry, I forgot to include errors:
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(12): error C2371: 'items' : redefinition; different basic types
1>
c:\users\aske\documents\c++\roguelike\roguelike\items.h(53) : see declaration of 'items'1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(36): error C2228: left of '.resize' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(36): error C2228: left of '.size' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(38): error C2228: left of '.size' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(41): error C2228: left of '.at' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(41): error C2228: left of '.category' must have class/struct/union
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(43): error C2228: left of '.at' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(43): error C2228: left of '.weaponCategory' must have class/struct/union
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(46): error C2228: left of '.at' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(46): error C2228: left of '.weaponCategory' must have class/struct/union
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(47): error C2228: left of '.at' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(47): error C2228: left of '.weaponCategory' must have class/struct/union
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(48): error C2228: left of '.at' must have class/struct/union
1> type is 'int'
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(48): error C2228: left of '.weaponCategory' must have class/struct/union
1>c:\users\aske\documents\c++\roguelike\roguelike\items.cpp(50): error C2228: left of '.at' must have class/struct/union