PDA

Pogledaj cijelu verziju : Globalne varijable



DragonSoul
29-06-2008, 13:41
Evo opet mene s mojim globalnim varijablama. Imam sljedeći source:

Global_vars.h

#pragma once

#ifndef GLOBAL_VARS_H
#define GLOBAL_VARS_H

#include "Item.h"

Item* Inventory[10];

#endif//GLOBAL_VARS_H

main.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Item.h"
#include "Global_vars.h"
using namespace std;

//Item* Inventory[10];

int main()
{
Item* item;
item = new Item("Dummy Weapon", "A description of an item", 1, 10, 100, 1000, 0, 1);
cout << "Ime: " << item->getName() << endl << "Opis: " << item->getDescription() << endl;
cout << "Cijena: " << item->getPrice() << endl;

cout << "Compile suscessful!\n";
cin.get();
}

Inventory.cpp

#pragma once

#ifndef INVENTORY_H
#define INVENTORY_H

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Item.h"
#include "Global_vars.h"
using namespace std;

#endif//INVENTORY_H

Ove druge klase nisu važne. Sad kad hoću kompajlirati javi mi:
1>g:\my documents\...\main.cpp(15) : error C2065: 'inventory' : undeclared identifier
Što da radim da to proradi?

EDIT: Uspio sam ono gore riješiti, ali sad mi linker priča gluposti. Kaže:
1>main.obj : error LNK2005: "class Item * * Inventory" (?Inventory@@3PAPAVItem@@A) already defined in Inventory.obj

heodox
29-06-2008, 14:19
moras koristit extern:

.h fajl:


extern int a;


.cpp fajl


int a = 1;

DragonSoul
29-06-2008, 14:37
A što ako ja Inventory[MAX_SLOTS]Splaniram koristiti u više .cpp-ova? Jel mi dovoljno onda njega čisto definirati u main.cpp-u, pa da ga mogu koristiti u drugim .cpp-ovima, ili? U suprotnom mi linker opet kaže multiple definition.

heodox
29-06-2008, 15:25
evo primjer:

[inventory.h]S - u .h fajl se obicno stavljaju samo deklaracije


class Items;

extern Items* Inventory[10];


[inventory.cpp]S- a u .cpp fajl implementacije


Items * Inventory[10];


sad inventory.h mozes includat gdje hoces i tamo koristit "Inventory":

[inventoryscreen.cpp]


#include "Inventory.h"

void showInventory()
{
for (int i = 0; i < MAXSIZE; i++)
...// Inventory[i]S(ispis)
}


[battlescreen.cpp]


#include "Inventory.h"

void useItemFromInventory(int ItemIndex)
{
switch( Inventory[InventoryIndex].type)
{
case POTION:
Character.Helt += Inventory[InventoryIndex].healing_power;
break;
}
}

DragonSoul
29-06-2008, 15:48
Hvala! (Trenutno) radi! Budem se javio ako budem opet imao problema.

DragonSoul
29-06-2008, 17:33
Evo, opet problem. Imam sljedeće fileove:
Global_vars.h

//#pragma once
//Ovaj file služi za definiranje globalnih varijabli
//#ifndef GLOBAL_VARS_H
//#define GLOBAL_VARS_H

#include "Item.h"
#include "Macros.h"

//------- Global system stuff -------
extern Item* Inventory[INVENTORY_SLOTS];
//-----------------------------------


//------- Quest item database -------

//-----------------------------------


//------- Weapons database -------
extern Item* DummyWeaponNone;
extern Item* DummyWeaponFire;
//--------------------------------


//------- Armor database -------

//------------------------------


//------- Ammo database -------

//-----------------------------


//------- Potion database -------

//-------------------------------


//------- Misc item database -------

//----------------------------------
//#endif//GLOBAL_VARS_H

Global_vars.cpp

#include "Global_vars.h"
#include "Macros.h"

//------- Global system stuff -------
Item* Inventory[INVENTORY_SLOTS];
//-----------------------------------


//------- Quest item database -------

//-----------------------------------


//------- Weapons database -------
Item* DummyWeaponNone = new Item("Dummy Weapon", "A description of an item", 1, 10, 100, 1000, 0, 1);
Item* DummyWeaponFire = new Item("Dummy Weapon Fire", "A hot item", 1, 10, 100, 3123, 1, 2);

//--------------------------------


//------- Armor database -------

//------------------------------


//------- Ammo database -------

//-----------------------------


//------- Potion database -------

//-------------------------------


//------- Misc item database -------

//----------------------------------

E, sad kad pokušam kompajlirati ostatak koda, on mi kaže "Run-Time Check Failure #3 - The variable 'DummyWeaponNone' is being used without being defined". Što da radim?

heodox
29-06-2008, 18:12
hmmm... ne vidim nis krivo s ovime, daj ostatak koda da ja probam

Luka
29-06-2008, 18:21
rješio je problem

DragonSoul
29-06-2008, 18:37
hmmm... ne vidim nis krivo s ovime, daj ostatak koda da ja probam

Problem je bio u tome što je u mainu bio definiran jedan Item pointer pod istim imenom. Onda je on (valjda) prije došao na snagu/overwriteao onaj iz headera, pa mi je debugger javljao koristim nedefiniran objekt.