moras koristit extern:
.h fajl:
.cpp fajlCode:extern int a;
Code:int a = 1;
Evo opet mene s mojim globalnim varijablama. Imam sljedeći source:
Global_vars.h
main.cppCode:#pragma once #ifndef GLOBAL_VARS_H #define GLOBAL_VARS_H #include "Item.h" Item* Inventory[10]; #endif//GLOBAL_VARS_H
Inventory.cppCode:#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(); }
Ove druge klase nisu važne. Sad kad hoću kompajlirati javi mi:Code:#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
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
moras koristit extern:
.h fajl:
.cpp fajlCode:extern int a;
Code:int a = 1;
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.
evo primjer:
[inventory.h]S - u .h fajl se obicno stavljaju samo deklaracije
[inventory.cpp]S- a u .cpp fajl implementacijeCode:class Items; extern Items* Inventory[10];
sad inventory.h mozes includat gdje hoces i tamo koristit "Inventory":Code:Items * Inventory[10];
[inventoryscreen.cpp]
[battlescreen.cpp]Code:#include "Inventory.h" void showInventory() { for (int i = 0; i < MAXSIZE; i++) ...// Inventory[i]S(ispis) }
Code:#include "Inventory.h" void useItemFromInventory(int ItemIndex) { switch( Inventory[InventoryIndex].type) { case POTION: Character.Helt += Inventory[InventoryIndex].healing_power; break; } }
Hvala! (Trenutno) radi! Budem se javio ako budem opet imao problema.
Evo, opet problem. Imam sljedeće fileove:
Global_vars.h
Global_vars.cppCode://#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
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?Code:#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 ------- //----------------------------------
hmmm... ne vidim nis krivo s ovime, daj ostatak koda da ja probam
rješio je problem
The candle flame gutters. Its little pool of light trembles. Darkness gathers. The demons begin to stir.
<3 <3 <3 <3 <3 <3 <3
ŠIRI SA MNOM LJUBAV Cherry!
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.Izvorno postao heodox