CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Apr 2009
    Posts
    8

    Structure Question.

    Hello, im new with Object oriented programming.
    I'm having a problem with a structure, what I'm trying to do is to make a function to create a sub structure named depending on what the input is, let me show what ive done.

    Code:
    items.h
    
    struct Weapon
    {
        // Strings
        string mName;
        string mQuality;
        
        // Integers
        int mItemID;
        int mGoldValue;
        int mDamageLow;
        int mDamageHigh;
    };
    
    items.cpp
    
    void Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh)
    {
        Weapon name;
        
        name.mName = name;
        name.mQuality = qual;
        name.mItemID = ID;
        name.mGoldValue = value;
        name.mDamageLow = dmgLow;
        name.mDamageHigh = dmgHigh;
    }
    Now, I get an error that says: (declaration of 'Weapon name' shadows a parameter ).

    Obviously I'm doing something wrong since im trying to make the parameter "name" be the name of the sub structure, for example: if user puts in "ShortSword" the name of it should be
    "ShortSword.mName", and so on...

    So is there any easier (working?) way to do this?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Structure Question.

    In your main function, you have both a string and a 'Weapon' object with the same identifier, 'name'
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    I believe that you need to use this syntax:

    Code:
    struct Weapon name;
    instead of
    Code:
    Weapon name;
    EDIT and probably also change the name of your Weapon to something as per Etherous.

    Duplicate variable names will cause quite a row with your compiler spitting all sorts of
    related and seemingly non-related error messages.
    Last edited by ahoodin; April 20th, 2009 at 05:16 PM.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    Another point is that you can both create a constructor for your struct and change your syntax to support a more simple variable declaration:

    Code:
    typedef struct Weapon {
    //...your stuff
    } WEAPON, *PWEAPON;
    then you can declare as such:

    Code:
    WEAPON myweapon;
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Structure Question.

    Might be more convenient in the long run to just do this:
    Code:
    typedef struct Weapon
    {
    ...
    } Weapon;
    Edit: ahoodin was faster
    Last edited by Etherous; April 20th, 2009 at 05:21 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    I believe that I mentioned that, although if you are referring to the use of capital letters, that appears to be the standard in books regarding the usage of typedef.
    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Structure Question.

    Quote Originally Posted by ahoodin View Post
    I believe that I mentioned that, although if you are referring to the use of capital letters, that appears to be the standard in books regarding the usage of typedef.
    Sorry. You must have posted yours before I'd posted mine saying the same thing
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    The really odd thing about all this is that I believe we appear to be posting at precisely the same errr....minute...lol I would not say second because it is not that precise.

    I feel like Kreyton the robot from Red Dwarf, my favorite tv show

    http://watchreddwarf.com/category/se...dwarf-season-1
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Structure Question.

    All the stuff about needing "struct" in the type and the typedefs to get around it and whatnot.....doesn't apply to C++. Only C. And since this struct contains a std::string, there's no chance of using it in a C program anyway.

  10. #10
    Join Date
    Apr 2009
    Posts
    8

    Re: Structure Question.

    Thanks for all the answers!

    I changed it some now, but ran into another problem.

    Heres the new code:

    items.h
    Code:
    #ifndef ITEMS_H
    #define ITEMS_H
    
    #include <string>
    using namespace std;
    
    struct Weapon
    {
        void Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh);
    
        // Strings
        string mName;
        string mQuality;
        
        // Integers
        int mItemID;
        int mGoldValue;
        int mDamageLow;
        int mDamageHigh;
    };
    
    #endif
    items.cpp
    Code:
    #include "items.h"
    #include <string>
    
    using namespace std;
    
    void Weapon::Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh)
    {
        mName = name;
        mQuality = qual;
        mItemID = ID;
        mGoldValue = value;
        mDamageLow = dmgLow;
        mDamageHigh = dmgHigh;
    }
    game.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
    #include "items.h"
    
    using namespace std;
    
    int Init()
    {
        Weapon ShortSword;
        ShortSword.Create("Short Sword", "Common", 1, 10, 1, 4);
    }
    
    int main()
    {
        srand(time(0));
        Init();
        
        cout << ShortSword.mName << endl;
        system("pause");
        return 0;
    }
    But now i get these errors:

    E:\Program\C++ Game Programming Files\Quillon\game.cpp In function `int main()':
    22 E:\Program\C++ Game Programming Files\Quillon\game.cpp `ShortSword' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    E:\Program\C++ Game Programming Files\Quillon\Makefile.win [Build Error] [game.o] Error 1


    Any suggestions?

  11. #11
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    Well at this point it appears to be a problem with your scope. Your ShortSword variable only has local scope to Init(). It needs to be upgraded to either class scope, or declared in main.
    ahoodin
    To keep the plot moving, that's why.

  12. #12
    Join Date
    Aug 2007
    Posts
    858

    Re: Structure Question.

    Obviously I'm doing something wrong since im trying to make the parameter "name" be the name of the sub structure, for example: if user puts in "ShortSword" the name of it should be
    "ShortSword.mName", and so on...
    What you want isn't possible in C++. Understand, the identifiers in your code "Weapon", "name", "mQuality", or whatever, don't even exist when the program runs. They are just used to make it easy for us lowly humans to read and understand code.

    If you want to identify something by a user defined name, you give it a string attribute to hold that name. Which you've already done. Or, depending on exactly what your needs are, you may to look at std::map.

  13. #13
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    I understood him quite well. As for being a lowly human, that is a career choice.
    (so I assume you speak for yourself)
    ahoodin
    To keep the plot moving, that's why.

  14. #14
    Join Date
    Apr 2009
    Posts
    8

    Re: Structure Question.

    So i need to make it into a class or declare in main?

    If i made, lets say 100 different Weapons, it would be a little too much code that goes into main, so isnt there any other way to make it into a function to make it look cleaner in main?

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Structure Question.

    It sounds to me like you want a std::map< std::string, Weapon >. That's the closest you can get to what you seem to be trying to do.

    Alternatively, you could look into inheritance, for instance defining ShortSword as a subclass of Weapon. That wouldn't be worthwhile unless you had a number of different ShortSword objects you expected to be manipulating with various properties.

    From your questions, I'm wondering whether you really understand what object scope is all about.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured