Thread: Passing a struct to a class

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    23

    Passing a struct to a class

    Hello everyone,

    i'm new to this forum, and new to c++ too. For this reason forgive any my mistake and (dumb) questions.

    Here my first question:

    I have a struct as follow:
    Code:
    struct Obj {
    
        string name;
        int num;
        string description;
    
    };
    
    Obj Object[100];
    Now, i would like to create a class named "character" that can hold one or more objects that i can give to him via the main class (just like some rpg game)

    How can i do this?

    My idea is to do something like that

    Character Bill;
    Bill.Object.push(object[1]);

    I tried in several ways, getting weird or non functional results.

    Thank you in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could make the Character class have a std::vector<Object> member variable named objects, and then provide it with an addObject member function that calls push_back on objects. Or, if this array of objects is supposed to be a universal list of all objects in the game, then you might use a std::vector<Object*> member variable instead.
    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
    Dec 2013
    Posts
    23
    Thank you for the reply.

    Could you, please, add the basic class structure for this?

    Thank you

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <vector>
    
    struct Obj {
        
        std::string name;
        int damage;
        std::string description;
        
    };
    
    
    
    int main(int argc, const char * argv[])
    {
        std::string name;
        int damage;
        std::string description;
        
        std::vector<Obj>monsters;
        
        Obj temp;
        std::cout<<"What is your monsters name:";
        getline(std::cin,name);
        
        std::cout<<"What is your monsters damage:";
        std::cin>>damage;
        
        std::cout<<"What is your monster look like:";
        getline(std::cin,description);
        
        temp.name = name;
        temp.damage=damage;
        temp.description=description;
        
        monsters.push_back(temp);
        
        return 0;
    }
    This is something you might can go off of..... Not what laser light said but this is a simple way to push structs into a vector. I am sure her way is better though.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @ laserlight Do you think <list> would be the best way to handle this? vector seems like a lot of trouble.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    Thank you.

    Btw i have to pass the struct of the Object to the class character.

    I created a basic class, and i've created a member variable std::vector<Object> objects;
    I get this error in the class character:

    character.h|11|error: 'Object' was not declared in this scope|
    character.h|11|error: template argument 1 is invalid|
    character.h|11|error: template argument 2 is invalid|

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by AmiPlus View Post
    Thank you for the reply.

    Could you, please, add the basic class structure for this?

    Thank you
    Break down what laserlight said piece by piece:

    Quote Originally Posted by laserlight
    You could make the Character class...
    Code:
    class Character
    {
        ...
    };
    Quote Originally Posted by laserlight
    have a std::vector<Object> member variable named objects, ...
    Code:
    #include <vector>
    
    class Character
    {
        std::vector<Object> objects;
        ...
    };
    Quote Originally Posted by laserlight
    and then provide it with an addObject member function ...
    Code:
    #include <vector>
    
    class Character
    {
        std::vector<Object> objects;
        ...
    public:
        void addObject(const Object& item);
    };
    
    void Character::addObject(const Object& item)
    {
        ...
    }
    Quote Originally Posted by laserlight
    that calls push_back on objects.
    Code:
    #include <vector>
    
    class Character
    {
        std::vector<Object> objects;
        ...
    public:
        void addObject(const Object& item);
    };
    
    void Character::addObject(const Object& item)
    {
        objects.push_back(item);
    }
    ...easy!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    This is what i've done, getting the errors described before:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class CCharacter{
        string _name, _genre;
        int _inRoom, _status;
        bool _activeCharacter;
    
      public:
        std::vector<Object> objects;
        CCharacter (string,string, int, bool, int);
        string getName () {return (_name);}
    
        void addObject(const Object& item);
    
    };
    
    void CCharacter::addObject(const Object& item)
    {
        objects.push_back(item);
    }
    
    CCharacter::CCharacter (string name, string genre,int inRoom, bool activeCharacter, int status) {
      _name=name;
      _genre = genre;
    
    }

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    character.h|11|error: 'Object' was not declared in this scope|

    Where is your structure declaration of Object

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The compiler needs to know what an "Object" is before it can generate the necessary code. There is nothing in the code you show which has the struct Obj (what we seem to have taken to calling Object instead) you cited in your original post. The solution therefore is to place the struct definition in your code prior to the class. Also, note that your class works with several std::string data members so you should also remember to include the <string> class.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    struct Object
    {
        string name;
        int num;
        string description;
    };
     
    class CCharacter{
        string _name, _genre;
        int _inRoom, _status;
        bool _activeCharacter;
     
      public:
        std::vector<Object> objects;
        CCharacter (string,string, int, bool, int);
        string getName () {return (_name);}
     
        void addObject(const Object& item);
     
    };
     
    void CCharacter::addObject(const Object& item)
    {
        objects.push_back(item);
    }
     
    CCharacter::CCharacter (string name, string genre,int inRoom, bool activeCharacter, int status) {
      _name=name;
      _genre = genre;
     
    }




    Incidentally, you should prefer initialization lists for construction and try to keep in mind const correctness, member functions which do not alter the state of the class they are called upon ("get" member functions for example) should be declared const.

    Code:
    class CCharacter{
        ...
        string getName () const {return (_name);}
        ... 
    };
    
    CCharacter::CCharacter (string name, string genre,int inRoom, bool activeCharacter, int status)
        : _name(name), _genre(genre), _inRoom(inRoom), _activeCharacter(activeCharacter), _status(status)
    {
    
    }
    Last edited by hk_mp5kpdw; 12-30-2013 at 02:03 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <vector>
    
     struct Obj {
        std::string name;
        int damage;
    };
    
    class objects {
    private:
        std::vector<Obj> Objects;
        
    public:
        objects();
        
        void addObject(std::vector<Obj>&);
    };
    
    objects::objects() { }
    
    void objects::addObject(std::vector<Obj> &MyObject)
    {
        
        Objects.push_back(MyObject); //No matching member function for call 'push_back'
    
    }
    int main(int argc, const char * argv[])
    {
        objects Myobjects;
        Obj tempObj;
        
        std::vector<Obj>newVector;
        int num=0, count=0,damage=0;
        std::string name;
        
        
        std::cout<<"How many objects do you have?\n";
        std::cin>>num;
        
        while(num!=count)
        {
            std::cout<<"what is your objects name?\n";
            getline(std::cin, name);
            
            std::cout<<"what is your objects damage level?\n";
            std::cin>>damage;
            
            tempObj.name = name;
            tempObj.damage = damage;
            Myobjects.addObject(newVector);
            
            count++;
        }
        
        
        for (auto it = newVector.begin(); it!=newVector.end(); ++it) {
            std::cout<<*it<<std::endl;
        }
        
        return 0;
    }
    What is wrong here. I decide to so it myself and it is not working.
    Last edited by jocdrew21; 12-30-2013 at 02:16 PM.

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Look at like 16 of your code for starters jocdrew21

    And don't forget your
    Code:
    #include <string>

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jocdrew21 View Post
    What is wrong here. I decide to so it myself and it is not working.
    You are trying to push "vector<Obj> MyObject" into "vector<Obj> Objects". You need to push a single Obj item into the vector, so change your function to accept a single item rather than a container of items.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
     struct Obj {
        std::string name;
        int damage;
    };
    
    class objects {
    private:
        std::vector<Obj> Objects;
        
    public:
        objects();
        
        void addObject(const Obj &);
        void print();
    };
    
    objects::objects() { }
    
    void objects::addObject(const Obj& MyObject)
    {
        
        Objects.push_back(MyObject); //No matching member function for call 'push_back'
    
    }
    
    void objects::print()
    {
        for (auto it = std::begin(Objects); it!=end(Objects); ++it) {
            std::cout<<*it<<std::endl;
        }
    }
    int main(int argc, const char * argv[])
    {
        objects Myobjects;
        Obj tempObj;
        
        int num=0, count=0,damage=0;
        std::string name;
        
        
        std::cout<<"How many objects do you have?\n";
        std::cin>>num;
        
        while(num!=count)
        {
            std::cout<<"what is your objects name?\n";
            std::cin.ignore();
            getline(std::cin, name);
            
            std::cout<<"what is your objects damage level?\n";
            std::cin>>damage;
            
            tempObj.name = name;
            Myobjects.addObject(tempObj);
            
            tempObj.damage = damage;
            Myobjects.addObject(tempObj);
            
            count++;
        }
        
        Myobjects.print();
        
        return 0;
    }
    Ahh ok.... But what the heck is going on with my print function? It only works if I put a & instead of a * like I am suppose to. However I have done this same way several times in the past but now it is not working.

  15. #15
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    @Ewiw

    Yes, i was thinking the same. Since nobody mentioned, i though that was unnecessary and, since this structure is present into the main class, i though that was also available (magically) in the character class. To tell the truth, i tested a version with the object structure before you remember me and, of course, worked better than the version without the structure. Still not working as it should, tho.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Class object to the same class method
    By infantheartlyje in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2011, 06:51 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Making a (atom) class in a (struct) class makes big errors!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-11-2007, 07:18 PM
  4. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 09:25 AM
  5. passing a struct to a class
    By major_small in forum C++ Programming
    Replies: 5
    Last Post: 02-29-2004, 04:58 PM