Thread: Structures... oh boy

  1. #1
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Structures... oh boy

    Yes, in my ever-stubborn quest for C++ knowlege, I have begun messing with structures (in which I should have before classes).

    I have a problem, though. I have declared variables in the structure, and I declared an instance of the structure outside the structure. It's saying the same thing for each instance outside, " expected constructor, destructor, or type conversion before '.' token," and for every variable in the instance, it says the variable is undeclared.

    I'll use the RPG example again, since that's what seems to give me the most problems...
    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    //START RACE STUFF*************************************************
    //START RACE STUFF*************************************************
    
    //START STRUCTURE
    
    struct Stats
    {
           void power (int pow);
           void vitality (int vit);
           void magic (int mag);
           void maxhealth (int maxh);
           void health (int hea);
           void maxmana (int maxm);
           void mana (int man);
           int displaypower();
           int displayvitality();
           int displaymagic();
           int displaymaxhealth();
           int displayhealth();
           int displaymaxmana();
           int displaymana();
           int strength;
           int defense;
           int intelligence;
           int maxhp;
           int hp;
           int maxmp;
           int mp;
           
    };
    
    //END STRUCTURE
    
    //START FUNCTION DECLARATION OF STRUCT
    
    void Stats::power (int pow)
    {
         strength=pow;
    }
    void Stats::vitality (int vit)
    {
         defense=vit;
    }
    void Stats::magic (int mag)
    {
         intelligence=mag;
    }
    void Stats::maxhealth (int maxh)
    {
         maxhp=maxh;
    }
    void Stats::health (int hea)
    {
         hp=hea;
    }
    void Stats::maxmana (int maxm)
    {
         maxmp=maxm;
    }
    void Stats::mana (int man)
    {
         mp=man;
    }
    int Stats::displaypower()
    {
        return strength;
    }
    int Stats::displayvitality()
    {
        return defense;
    }
    int Stats::displaymagic()
    {
        return intelligence;
    }
    int Stats::displaymaxhealth()
    {
        return maxhp;
    }
    int Stats::displayhealth()
    {
        return hp;
    }
    int Stats::displaymaxmana()
    {
        return maxmp;
    }
    int Stats::displaymana()
    {
        return mp;
    }
    
    //END FUNCTION DECLARATION OF STRUCT
    
    //START RACE SELECTION
    
    //1-human 2-dwarf 3-elf
    
    //human
    Stats human;
    human.power(10);
    human.vitality(10);
    human.magic(10);
    human.maxhealth(100);
    human.health(100);
    human.maxmana(10);
    human.mana(10);
    
    //dwarf
    Stats dwarf;
    dwarf.power(15);
    dwarf.vitality(15);
    dwarf.magic(5);
    dwarf.maxhealth(150);
    dwarf.health(150);
    dwarf.maxmana(5);
    dwarf.mana(5);
    
    //elf
    Stats elf;
    elf.power(5);
    elf.vitality(10);
    elf.magic(15);
    elf.maxhealth(50);
    elf.health(50);
    elf.maxmana(15);
    elf.mana(15);
    
    //hero
    Stats hero;
    
    //class integers and functions
    int char_race =1;
    
    void race_stats()
    {
         switch(char_race)
         {
                          case 1:
                               hero.power(human.power(pow));
                               hero.vitality(human.vitality(vit));
                               hero.magic(human.magic(mag));
                               hero.maxhealth(human.maxhealth(maxh));
                               hero.health(human.health(hea));
                               hero.maxmana(human.maxmana(maxm));
                               hero.mana(human.mana(man));
                               break;
                          case 2:
                               hero.power(dwarf.power(pow));
                               hero.vitality(dwarf.vitality(vit));
                               hero.magic(dwarf.magic(mag));
                               hero.maxhealth(dwarf.maxhealth(maxh));
                               hero.health(dwarf.health(hea));
                               hero.maxmana(dwarf.maxmana(maxm));
                               hero.mana(dwarf.mana(man));
                               break;
                          case 3:
                               hero.power(elf.power(pow));
                               hero.vitality(elf.vitality(vit));
                               hero.magic(elf.magic(mag));
                               hero.maxhealth(elf.maxhealth(maxh));
                               hero.health(elf.health(hea));
                               hero.maxmana(elf.maxmana(maxm));
                               hero.mana(elf.mana(man));
                               break;
         }
    }
    
    //END RACE SELECTION
    
    
    //END RACE STUFF***************************************************
    //END RACE STUFF***************************************************
    
    int main(int argc, char *argv[])
    {
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Try compiling it and you'll see what I mean. It's got me stumped... any tips?
    ------------------------------------------------
    cout<<"Hello World!\n";
    ------------------------------------------------

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have begun messing with structures (in which I should have before classes).
    Suppose you changed
    Code:
    //START STRUCTURE
    
    struct Stats
    {
    to
    Code:
    //START CLASS
    
    class Stats
    {
    public:
    Would you then know what's wrong with your program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The code between the comments "//human" and "//hero" includes executable statements, which should ONLY ever exist inside a function body, rather than at file scope (outside of any function)

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    You have the classic form of a class construct not a struct.


    This may help as well
    • Members in a structure are public by default whereas they are private by default in a class.
    Mr. C: Author and Instructor

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You shouldnt or CANT have those function statements outside of a function (I believe I mentioned this last topic), like main().

    To test, move all those class objects/functions inside main() and delete those last 2 functions overtop of main and it will compile.

    If you need to use those that two functions, you will have to pass a REFERENCE TO THE OBJECTS (PARAMETERS).

    Man... your program sure got ugly since your last topic, where I explained how to do what you requested, and you turn around and make everything global.

    I also explained last topic that the way you are setting numbers to your classes will not work.

    Look.. the method 'maxhealth' is how you set a value right. so saying hero.maxhealth(100) will set his health to 100. maxhealth function does not return anything, so calling it and assigning it to a variable will give you nothing. int variable = elf.maxhealth(50), that will set the health to 50, but variable will be 0. So if you are putting that function as a parameter to set health, you are going to get nothing.

    Code:
    hero.maxhealth(elf.maxhealth(maxh));
    That is incorrect on so many levels.

    You want this, and only this:

    Code:
    hero.maxhealth(elf.displayhealth());
    displayhealth() returns the health value, and its inside the parameters of hero.maxhealth() where it is assigned to the hero objects health.

    And structures only have 1 difference from classes in C++, structures members are public by default, and classes are private, nothing else, that minor difference is all (which isnt your problem, you should go read a few more tutorials, check pointers, references, return values, parameters, and classes (the code from the last topic was much better - after I pointed out the errors)).
    Last edited by Dae; 10-02-2005 at 02:42 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    I don't know much about structures other than they are like classes, except for the default members are public, but it seems what your trying to do would be easier done in a class. Just make the variables such as pow,vit,mag, hea private members in the class and use the displaywhatever() to get the values for those variables.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by RealityFusion
    I don't know much about structures other than they are like classes, except for the default members are public, but it seems what your trying to do would be easier done in a class. Just make the variables such as pow,vit,mag, hea private members in the class and use the displaywhatever() to get the values for those variables.
    You're barking up the wrong tree. If it doesn't work in a struct, it won't work in a class either. The only difference between a struct and a class in C++ is the default access to members, and the default inheritence attrribute (for a struct, both are public, for a struct both are private). So
    Code:
    class X
    {
         public:
           // whatever
    };
    and
    Code:
    struct X
    {
           // whatever
    };
    are functionally equivalent.

    The original problem is because there is some code placed outside any function, and it needs to be placed in a function. It has nothing to do with the fact that the code uses a C++ struct rather than a C++ class.

  8. #8
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by grumpy
    You're barking up the wrong tree. If it doesn't work in a struct, it won't work in a class either. The only difference between a struct and a class in C++ is the default access to members, and the default inheritence attrribute (for a struct, both are public, for a CLASS both are private). So
    Well I wasn't trying to say that what he is doing in the struct would work in a class. I was trying to say that it can be done a different way, easier imo, with a class. Something like this:
    Code:
    class stats
    {
    public:
             string getRace();
             int displaystats();
    private:
            int pow;
            int vit;
            int mag;
            string Race;
    }
    
    stats::getRace()
    {
    int x;
    cout <<"Pick a race" <<endl;
    cout  <<"1 - Elf" <<endl;
    cout <<"2 - Human" <<endl;
    cout <<"3 - Dwarf" <<endl;
    cin >>x;
    cin.ignore();
    switch (x)
    {
    case 1:
          Race = "Elf";
          break;
          // put the rest of the options here
    }
    }
    stats::displaystats()
    {
    if (Race == "Elf")
    {
    pow = 5;
    vit = 10;
    cout <<pow <<endl;
    cout <<vit <<endl;
    }
    // either continue with if's or use another switch.
    }
    I guess you could do the same thing with a structure? Im just used to using class's instead of structures.
    Knowledge is power and I want it all

    -0RealityFusion0-

  9. #9
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Thanks for all the help. I've been searching for reference tutorials that are good, and I've came up pretty much empty handed so far. I happened to be in Jonesboro yesterday and bought a book on C++, hopefully one that'll clear most of this up.

    I'm learning, and I'm glad you guys are pretty tolerant of my... hehehe glorious absense of knowledge. I'm just trying to get all this stuff down before my Junior year, which I'm really going to start programming.

    Anyway, thanks again. I'll read some more tuts, practice some more, build myself up, and maybe in a few months I'll have it down.
    ------------------------------------------------
    cout<<"Hello World!\n";
    ------------------------------------------------

  10. #10
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    From my perspective, a couple things are wrong with what you have done.

    Firstly, a structure is to contain data, and then you have functions (possible of the global scope but not member functions of the structure) to operate on the data.

    A class, contains both. the data and the functions to operate on the data. This is known as encapsulation, meaning you hide the method in which data is operated on.

    What might be a better way to approach this problem would be:
    Take your structure, stats, and make a class, lets say Player.
    You then make a vaible within player that is your structure, and then create the methods for Player which, operator on the stats data.

    There are better ways to approach this, for instance, you create the class stats, which contains both the functions to operate on the data,
    and the data itself. then you Inherit From stats into a Class person. This would be usefull because you could suddenly create a class Monster, inherit Stats to it, and then just fill the whats needed and different for a monster.

    In short. Use your Structs for data, and Classes for data that has methods for ways to operate on the data.

    A c++ book i'd recommend, which is more of a tutorial then a refference, Accelerate c++ . It will get you primed on many topics, including Classes and their uses.

  11. #11
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Code:
    #include <iostream>
    using namespace std;
    
    //START STATS CLASS
    class Stats
    {
          public:
                 Stats();
                 ~Stats();
                 
                 void setStrength(int str);
                 int showStrength();
                 void setDefense(int def);
                 int showDefense();
                 void setSpeed(int spd);
                 int showSpeed();
                 void setMagic(int mag);
                 int showMagic();
          private:
                  int strength;
                  int defense;
                  int speed;
                  int magic;
    };
    //END STATS CLASS
    
    //STATS FUNCTIONS DECLARACTIONS START
    
    Stats::Stats(){};
    Stats::~Stats(){};
    void Stats::setStrength(int str) { strength = str; }
    int Stats::showStrength() { return strength; }
    void Stats::setDefense(int def) { defense = def; }
    int Stats::showDefense() { return defense; }
    void Stats::setSpeed(int spd) { speed = spd; }
    int Stats::showSpeed() { return speed; }
    void Stats::setMagic(int mag) { magic = mag; }
    int Stats::showMagic() { return magic; }
    
    //STATS FUNCTIONS DECLARATIONS END
    
    int main()
    {
        Stats * dwarf = new Stats; //dwarf stats
        dwarf->setStrength(12);
        dwarf->setDefense(10);
        dwarf->setSpeed(5);
        dwarf->setMagic(2);
        
        Stats * elf = new Stats; //elf stats
        elf->setStrength(2);
        elf->setDefense(5);
        elf->setSpeed(7);
        elf->setMagic(14);
        
        Stats * human = new Stats; //human stats
        human->setStrength(7);
        human->setDefense(6);
        human->setSpeed(8);
        human->setMagic(7);
        
        cout<<"Class:\t\tDwarf\t\tElf\t\tHuman\n"; //classes
        
        cout<<"Strength:\t" //begin strength
        <<dwarf->showStrength()
        <<"\t\t"
        <<elf->showStrength()
        <<"\t\t"
        <<human->showStrength()
        <<endl;
        cout<<"Defense:\t" //begin defense
        <<dwarf->showDefense()
        <<"\t\t"
        <<elf->showDefense()
        <<"\t\t"
        <<human->showDefense()
        <<endl;
        cout<<"Speed:\t\t" //begin speed
        <<dwarf->showSpeed()
        <<"\t\t"
        <<elf->showSpeed()
        <<"\t\t"
        <<human->showSpeed()
        <<endl;
        cout<<"Magic:\t\t" //begin magic
        <<dwarf->showMagic()
        <<"\t\t"
        <<elf->showMagic()
        <<"\t\t"
        <<human->showMagic()
        <<endl;
        
        //FREE MEMORY ALLOCATED
        delete dwarf;
        delete elf;
        delete human;
        //FREE MEMORY ALLOCATED
        
        cin.get();
        return 0;
    }
    This any better, guys?

    I've been reading about a hundred pages a day lately, and I've been sick from school for about 4 school days, so my progress is, well, progressing.

    I'm going to work on a way to select your class tonight. Then I'll build on that with the levelUp function, and so on and so forth until I've grasped these simple concepts. Like I've mentioned before, any tips or guidance is greatly appreciated.

    Edit: Moving future posts to game programming board, since it's slowly making its way towards there anyway
    Last edited by nickodonnell; 10-04-2005 at 06:54 PM.
    ------------------------------------------------
    cout<<"Hello World!\n";
    ------------------------------------------------

  12. #12
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Looks good.

    #tip: you arent meant to be a celebrity, so dont try to go global, youll just waste your time
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM