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

    Re: Structure Question.

    Quote Originally Posted by Lindley View Post
    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.
    No Im not sure, I've just started to learn C++ and its not in the book I have (Game Institute, Game Programming 1).

    But could you give me a link to an explanation / example?

  2. #17
    Join Date
    Apr 2009
    Posts
    8

    Re: Structure Question.

    Hm, now I just changed some code and it works.

    Earlier:
    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;
        system("pause");
        return 0;
    }
    Changed To:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
    #include "items.h"
    
    using namespace std;
    
    int main()
    {
        srand(time(0));
        
        Weapon ShortSword;
        ShortSword.Create("Short Sword", "Common", 1, 10, 1, 4);
        
        cout << ShortSword.mName;
        
        system("pause");
        return 0;
    }
    So basically all I did was to create the structure in main, instead of in a function with the creation that is called in main.

    Can someone explain why it works to create it in main, but not in a function which is called in main?

    Thanks.

  3. #18
    Join Date
    Aug 2007
    Posts
    858

    Re: Structure Question.

    Quote Originally Posted by ahoodin View Post
    I understood him quite well.
    Apparently not, since it took 12 posts for someone to address what he was actually wanting to do.

    As for being a lowly human, that is a career choice.
    (so I assume you speak for yourself)
    Unless you happen to be fluent in machine code, you're a lowly human in this context.

    Can someone explain why it works to create it in main, but not in a function which is called in main?
    http://en.wikipedia.org/wiki/Scope_(programming)
    http://en.wikipedia.org/wiki/Variabl...ope_and_extent

    If the book you're learning from has gotten you into functions and structures without explaining something as basic as scope, I'd really suggest looking for another book.
    Last edited by Speedo; April 20th, 2009 at 06:32 PM.

  4. #19
    Join Date
    Apr 2009
    Posts
    8

    Re: Structure Question.

    I do understand scopes, but what im asking is: do you have to create structures in main like this:

    Code:
    int main()
    {
        Weapon sword;
        Weapon dagger;
        Weapon axe;
        Weapon bow;
    }
    And is it impossible to create them in void functions which is called in main when to create them?


    Code:
    void Call();
    
    int main()
    {
       Call();
    }
    
    void Call()
    {
        Weapon sword;
        Weapon dagger;
        Weapon axe;
        Weapon bow;
    }
    Its hard to explain it when everyone is thinking so complex.

  5. #20
    Join Date
    Aug 2007
    Posts
    858

    Re: Structure Question.

    Code:
    void Call( )
    {
      Weapon sword;
      Weapon dagger;
      Weapon axe;
      Weapon bow;
    } // <-- sword, dagger, axe and bow go out of scope at this point, and no longer exist
    There are various methods to have a function create objects that are useable after that function ends. Namely, have the function take some STL container by reference and insert the objects into that container.

    Simple example using a map:

    Code:
    typedef std::map<std::string, Weapon> WeaponMap;
    
    void Create(WeaponMap& wm)
    {
      wm["sword"] = Weapon(/*params*/);
      wm["dagger"] = Weapon(/*params*/);
      // etc
    }
    
    int main( )
    {
      WeaponMap weps;
      
      Create(weps);
      
      cout << weps["sword"].mName;
      
      return 0;
    }
    Last edited by Speedo; April 20th, 2009 at 07:00 PM.

  6. #21
    Join Date
    Apr 2009
    Posts
    8

    Re: Structure Question.

    I think I understand how it works now, thanks mate.

  7. #22
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Structure Question.

    Quote Originally Posted by Speedo View Post
    Unless you happen to be fluent in machine code, you're a lowly human in this context.
    I'm afraid I don't care for the context. Speak for yourself mate.

    Quote Originally Posted by Speedo View Post
    Apparently not, since it took 12 posts for someone to address what he was actually wanting to do.
    Well again I am afraid this is a matter of opinion.
    ahoodin
    To keep the plot moving, that's why.

Page 2 of 2 FirstFirst 12

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