Prikazujem rezultate 1 do 9 od 9

Tema: Globalne varijable

  1. #1
    Senior Member Lord of The Fleas
    Datum registracije
    Jan 2007
    Lokacija
    /dev /null
    Postova
    412

    Globalne varijable

    Evo opet mene s mojim globalnim varijablama. Imam sljedeći source:

    Global_vars.h
    Code:
    #pragma once
    
    #ifndef GLOBAL_VARS_H
    #define GLOBAL_VARS_H
    
    #include "Item.h"
    
    Item* Inventory[10];
    
    #endif//GLOBAL_VARS_H
    main.cpp
    Code:
    #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
    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
    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

  2. #2

    Re: Globalne varijable

    moras koristit extern:

    .h fajl:
    Code:
    extern int a;
    .cpp fajl
    Code:
    int a = 1;

  3. #3
    Senior Member Lord of The Fleas
    Datum registracije
    Jan 2007
    Lokacija
    /dev /null
    Postova
    412

    Re: Globalne varijable

    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.

  4. #4

    Re: Globalne varijable

    evo primjer:

    [inventory.h]S - u .h fajl se obicno stavljaju samo deklaracije
    Code:
    class Items;
    
    extern Items* Inventory[10];
    [inventory.cpp]S- a u .cpp fajl implementacije
    Code:
    Items * Inventory[10];
    sad inventory.h mozes includat gdje hoces i tamo koristit "Inventory":

    [inventoryscreen.cpp]
    Code:
    #include "Inventory.h"
    
    void showInventory()
    {
    for (int i = 0; i < MAXSIZE; i++)
        ...//  Inventory[i]S(ispis)
    }
    [battlescreen.cpp]
    Code:
    #include "Inventory.h"
    
    void useItemFromInventory(int ItemIndex)
    {
    switch( Inventory[InventoryIndex].type)
    {
    case POTION:
    Character.Helt += Inventory[InventoryIndex].healing_power;
    break;
    }
    }

  5. #5
    Senior Member Lord of The Fleas
    Datum registracije
    Jan 2007
    Lokacija
    /dev /null
    Postova
    412

    Re: Globalne varijable

    Hvala! (Trenutno) radi! Budem se javio ako budem opet imao problema.

  6. #6
    Senior Member Lord of The Fleas
    Datum registracije
    Jan 2007
    Lokacija
    /dev /null
    Postova
    412

    Re: Globalne varijable

    Evo, opet problem. Imam sljedeće fileove:
    Global_vars.h
    Code:
    //#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
    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 -------
    
    //----------------------------------
    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?

  7. #7

    Re: Globalne varijable

    hmmm... ne vidim nis krivo s ovime, daj ostatak koda da ja probam

  8. #8
    Senior Member Master Blaster
    Datum registracije
    Nov 2005
    Lokacija
    Zadar
    Postova
    4.028

    Re: Globalne varijable

    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!

  9. #9
    Senior Member Lord of The Fleas
    Datum registracije
    Jan 2007
    Lokacija
    /dev /null
    Postova
    412

    Re: Globalne varijable

    = Citat = Izvorno postao heodox
    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.

Slične teme

  1. PHP kombinacija varijable i statične mail adrese
    By McPingvin_v2.0 in forum Programiranje i izrada igara
    Odgovora: 11
    Posljednji post: 11-08-2011, 23:20
  2. n00b pitanje- varijable
    By medusa in forum Programiranje i izrada igara
    Odgovora: 8
    Posljednji post: 21-12-2007, 18:02

Pravila postanja

  • Ne možeš stvarati nove teme
  • Ne možeš odgovarati na postove
  • Ne možeš slati privitke
  • Ne možeš mijenjati svoje postove
  •