Classes uitlezen

Status
Niet open voor verdere reacties.

pom86

Gebruiker
Lid geworden
28 jul 2007
Berichten
9
Mijn idee was om een turn bassed game te maken voor console.
Waarom ik dit wil doen is om beter c++ te kunnen/leren
met een instelling van laten we gewoon ergens aan beginnen en we zien wel waar we stranden.

Nou is mijn probleem als volgt:

Ik maak classes aan voor units maar nou is het probleem dat ik die niet kan uitlezen in een andere functie.
Hoe los ik dit op:

Units class:
PHP:
   //units
class units {
public:
       units();       //constructor om geheugen op te eissen
       ~units();      //deconstructor om het geheugen weer vrij te maken.
       int atk;       //How mutch attack power
       int hp;        //How mutch damage he can take
       int crit;      //what change on a criticall attack
       int block;     //Blocking someone elses attack
       int type;      //what kind of damage the unit does (1: melee/2: ranged/ 3: magic)
       int speed;     //How mutch attacks he can do in 1 turn
       int walk;      //How mutch fields he can walk
};
units::units() {      //contructor of units
        
}

units::~units() {     //deconstructor of units
        
}


Units aanmaken:
PHP:
//creating untis and there stats
int Cr_units() {
    system("cls");
    cout<<"Creating new game \n" ;
    cout<<"Creating counter x: DONE \n";
    cout<<"Creating counter y: DONE \n";
    cout<<"Creating units: ";
    Ca_procent( 1, 4);
    
    //first job classes
    units swordsman;                       //fighting with a 1 handed sword and shield
    swordsman.atk = 5;                     //How mutch attack power
    swordsman.hp = 40;                     //How mutch damage he can take
    swordsman.crit = 30;                   //what change on a criticall attack
    swordsman.block = 0;                   //chance to block someones attack
    swordsman.type = 1;                    //what kind of damage the unit does (1: melee/ 2: ranged/ 3: magic)
    swordsman.speed = 5;                   //How mutch attacks he can do in 1 turn
    swordsman.walk = 12;                   //How mutch fields he can walk
    
    system("cls");
    cout<<"Creating new game \n" ;
    cout<<"Creating counter x: DONE \n";
    cout<<"Creating counter y: DONE \n";
    cout<<"Creating units: ";
    Ca_procent( 2, 4);
    
    units thief;                       //fighting with a dagger makes him slightly faster then a knight
    thief.atk = 3;                     //How mutch attack power
    thief.hp = 35;                     //How mutch damage he can take
    thief.crit = 40;                   //what change on a criticall attack
    thief.block = 0;                   //chance to block someones attack
    thief.type = 1;                    //what kind of damage the unit does (1: melee/ 2: ranged/ 3: magic)
    thief.speed = 7;                   //How mutch attacks he can do in 1 turn
    thief.walk = 8;                   //How mutch fields he can walk
    
    system("cls");
    cout<<"Creating new game \n" ;
    cout<<"Creating counter x: DONE \n";
    cout<<"Creating counter y: DONE \n";
    cout<<"Creating units: ";
    Ca_procent( 3, 4);
    
    units mage;
    mage.atk = 12;                     //How mutch attack power
    mage.hp = 20;                     //How mutch damage he can take
    mage.crit = 25;                   //what change on a criticall attack
    mage.block = 0;                   //chance to block someones attack
    mage.type = 3;                    //what kind of damage the unit does (1: melee/ 2: ranged/ 3: magic)
    mage.speed = 4;                   //How mutch attacks he can do in 1 turn
    mage.walk = 10;                   //How mutch fields he can walk
    
    system("cls");
    cout<<"Creating new game \n" ;
    cout<<"Creating counter x: DONE \n";
    cout<<"Creating counter y: DONE \n";
    cout<<"Creating units: ";
    Ca_procent( 4, 4);
    
    units cleric;
    cleric.atk = 0;                     //How mutch attack power
    cleric.hp = 25;                     //How mutch damage he can take
    cleric.crit = 0;                   //what change on a criticall attack
    cleric.block = 20;                   //chance to block someones attack
    cleric.type = 3;                    //what kind of damage the unit does (1: melee/ 2: ranged/ 3: magic)
    cleric.speed = 7;                   //How mutch attacks he can do in 1 turn
    cleric.walk = 10;                   //How mutch fields he can walk
    
    //second job classes(future version)
    
    //third job classes(future version)
    
    system("cls");
    cout<<"Creating new game \n" ;
    cout<<"Creating counter x: DONE \n";
    cout<<"Creating counter y: DONE \n";
    cout<<"Creating units: DONE \n ";    
}

En het uitlezen van de classes:
PHP:
int R_units() { 
   
    cout<<"Unit name \t atk \t hp \t crit \t block \t type \t speed \t walk \n  ";
    cout<<"Swordsman \t"<< swordsman.atk <<"\t"<< swordsman.hp ;
    cin.get();
    /*
    swordsman.atk
    swordsman.hp
    swordsman.crit
    swordsman.block
    swordsman.type
    swordsman.speed
    swordsman.walk
    */
}

En dit laatste werkt dus niet.
De error die die geeft is dat swordsman een first useage/undecleared function is.
Hoe zorg ik ervoor dat hij verwijst naar die class zodat hij weet waar hij de informatie vandaan moet halen.
 
Laatst bewerkt:
hoe weet die functie wat swordsman is? geef hem het mee als attribute of inline werkt ook toch? weet niet eigenlijk moet je maar even proberen
 
hoe weet die functie wat swordsman is? geef hem het mee als attribute of inline werkt ook toch? weet niet eigenlijk moet je maar even proberen

Daar snapte ik niks van

Ik zal eht anders uitleggen wat mijn probleem is.

Het probleem is dat andere functies van die data gebruik moeten kunnen maken dus het kunnen uitlezen.
In principe komt het erop neer dat je van een lokale variable een globale variable maakt.

Dingen die ik al geprobeerd heb:
PHP:
cout<<"Swordsman \t"<< units.swordsman.atk <<"\t"<< units.swordsman.hp ;
Meschiend at dit een beter beeld geeft vanw at mijn probleem is.
 
Code:
int R_units( units thisunit );
int R_units( units thisunit ) { 
    
    cout<<"Unit name \t atk \t hp \t crit \t block \t type \t speed \t walk \n  "; 
    cout<<"Swordsman \t"<< thisunit.atk <<"\t"<< thisunit.hp ; 
    cin.get(); 
}
int main()
{
units swordsman;
R_units( swordsman );
}
snap je t zo? code>woorden
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan