CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Dec 2011
    Posts
    11

    problem with push_back

    Hi! I'm not sure about using push_back properly in a program I need to get going:

    Code:
    vector <string> myvector;             // here I'm not sure 
    
    person::person(string n, int a)
    {
    	name=n;
    	age=a;
    
    	set_person(n);
    }
    
    int person::get_age()
    {
    	return age;
    }
    
    string person::get_name() const
    {
    	return name;
    }
    
    int person::update_age()
    {
    	return age+1;
    }
    
    person* person::set_person(string n)
    {
    	myvector.push_back(n);            //it compiles that way
                                              //but I think I don't get what I'm supposed to
    	                                  //
                                              //from my previous example; without line 13!
                                              //people.push_back(n);      
    }
    header:
    Code:
    #ifndef PERSON_H
    #define PERSON_H
    
    using namespace std;
    class person
    {
    public:
    	vector<person*> people;
    	person(string,int);
    	person* set_person(string);
    	string name;
    	int age;
    	int get_age();
    	int update_age();
    	string get_name()const;
    
    };
    #endif
    main:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>	
    ///////////////////////////////////////////
    
    #include <iomanip>
    #include <vector>
    #include "person.h"
    
    using namespace std;
    
    int main()
    {
    	vector<person*> people;
    
    	bool more=true;
    	while(more)
    	{
    	cout<<"Enter name, q to quit: ";
            
    	string name;
    	getline(cin,name);
            
            int age;
            
    	if(name=="q")
    	{
    		more=false;
    	}
            
    	else
    	{
    		people.push_back(new person(name, age));
    
    	}
    	}
            
    
    	for(int i=0; i<people.size(); i++)
    	{
    		cout<<(*people[i]).get_name();
    	}
            
            
            while(!people.empty())
            {
            delete people.back();
            people.pop_back();
            }
    system("pause");
    return 0;
    }
    In the version I used people.push_back(n); I got following failure message:

    Code:
    person.cpp:40: error: no matching function for call to 'std::vector<person*, std::allocator<person*> >::push_back(std::string&)'
    /usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = person*, _Alloc = std::allocator<person*>]
    kind regards,
    Fellow

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem with push_back

    Quote Originally Posted by Fellow View Post
    Hi! I'm not sure about using push_back properly in a program I need to get going:
    It isn't just push_back() that has issues.
    Code:
    person* person::set_person(string n)
    {
    	myvector.push_back(n);            //it compiles that way
                                              //but I think I don't get what I'm supposed to
    	                                  //
                                              //from my previous example; without line 13!
                                              //people.push_back(n);      
    }
    Why does this function say it returns a pointer to a person? I see no return value at all.
    Code:
    #ifndef PERSON_H
    #define PERSON_H
    
    using namespace std;
    A header should not have "using namespace std". The reasons have been discussed here many times, the main one being that if I were to include your header in my C++ module, my module is forced to use the entire std namespace.

    Instead, prepend all of the items that are in the std namespace with std::
    Code:
    #ifndef PERSON_H
    #define PERSON_H
    
    #include <vector>
    
    class person
    {
    public:
    	std::vector<person*> people;
    //...
    };
    Code:
    int person::update_age()
    {
    	return age+1;
    }
    This doesn't update age. All it does is returns the current age + 1. The current age is not updated.
    person.cpp:40: error: no matching function for call to 'std::vector<person*, std::allocator<person*> >:ush_back(std::string&)'
    /usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = person*, _Alloc = std::allocator<person*>]
    Why are you trying to add a string to a vector that takes a person pointer?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2011
    Posts
    11
    I tried:
    Code:
    string person::set_person(string n)
    {
    	people.push_back(n);            //it compiles that way
                                              //but I think I don't get what I'm supposed to
    	return n;                     //
                                              //from my previous example; without line 13!
                                              //people.push_back(n);      
    }
    and also:
    Code:
    void person::set_person(string n)
    {..
    but I don't find the fault

    I think I'm on the wrong track.
    How do I tackle this problem in a skilful way?

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

    Re: problem with push_back

    Your error there has nothing to do with the return type of the function, that's a separate issue.

    You can't push a std::string into a vector of person*.

  5. #5
    Join Date
    Oct 2007
    Posts
    34

    Re: problem with push_back

    Usually setter functions take a const reference and return void.

    For example:
    Code:
    void person::set_person(const string& n)
    {
    ...
    }

    I'm not sure if the intention of set_person is to set the person objects name or do something with the storing person objects in the vector.

    You have two of vector of person pointers
    Code:
    vector<person*> people;
    one in main and one in your person class declaration and a vector of strings.

    Hope this helps.
    Last edited by Yadrif; January 27th, 2012 at 08:35 AM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: problem with push_back

    I'd wonder why the person class has a vector of type person called people.

    What is myVector supposed to accomplish?

    I think you need to step back and think about what you're trying to do.

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

    Re: problem with push_back

    Quote Originally Posted by GCDEF View Post
    I'd wonder why the person class has a vector of type person called people.
    Perhaps the person is pregnant. With twins.

  8. #8
    Join Date
    Dec 2011
    Posts
    11

    Talking Re: problem with push_back

    yes, that would be nice

  9. #9
    Join Date
    Dec 2011
    Posts
    11

    Re: problem with push_back

    Now I have the following got going:
    Code:
    HEADER
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    
    /////////////////////////////
    #include <vector>
    
    #ifndef PERSON_H
    #define PERSON_H
    
    class person
    {
    public:
    	std::vector<person*> people;
    	person(std::string,int);
    	void person::set_person(const std::string& n);//person* set_person(std::string);
    	std::string name;
    	int age;
    	int get_age();
    	int update_age();
    	std::string get_name()const;
    
    };
    #endif 
    
    CPP
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    
    
    //////////////////////////////////
    #include <vector>
    #include "person.h"
    
    using namespace std;
    
    
    person::person(string n, int a)
    {
    	name=n;
    	age=a;
    
    	set_person(n);
    }
    
    int person::get_age()
    {
    	return age;
    }
    
    string person::get_name() const
    {
    	return name;
    }
    
    int person::update_age()
    {
    	return age+1;
    }
    
    void person::set_person(const string& n)
    {  
    	vector<person*> push_back(const string& n);              
    }
                       
    MAIN
    #include <cstdlib>
    #include <iostream>
    #include <string.h>	
    ///////////////////////////////////////////
    
    #include <iomanip>
    #include <vector>
    #include "person.h"
    
    using namespace std;
    
    int main()
    {
    	vector<person*> people;
    
    	bool more=true;
    	while(more)
    	{
    	cout<<"Enter name, q to quit: ";
            
    	string name;
    	getline(cin,name);
            
            int age;
            
    	if(name=="q")
    	{
    		more=false;
    	}
            
    	else
    	{
    		people.push_back(new person(name, age));
    
    	}
    	}
            
    
    	for(int i=0; i<people.size(); i++)
    	{
    		cout<<(*people[i]).get_name();
    	}
            
            
            while(!people.empty())
            {
            delete people.back();
            people.pop_back();
            }
    return 0;
    }
    Well, it works somehow.

    But honestly I don't know what I'm gathering from all this vector stuff!

    Is there something that could be done different? something more worthy to be called engineering ^^

    Thanks for the help, by the way

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

    Re: problem with push_back

    Code:
    	vector<person*> push_back(const string& n);
    That's not calling a method, that's declaring a function.

    Let's start simple: Why do you have a vector of person* called people within your person class? What is this intended to represent?

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: problem with push_back

    Why is there a people vector inside your person class?
    I don't understand what your set_person function is trying to do.
    You never get a value for age when you pass it to the person constructor.

  12. #12
    Join Date
    Dec 2011
    Posts
    11

    Re: problem with push_back

    neither do I.
    I took the code from any site and tried to grasp what vector is doing.
    It was not explained what they are up to do with the people vector!

    I didn't care much of this "std::vector<person*> people;" and hoped it would make sense in the end.

    Might appear strange, i know. I've just been searching for sites explaining how to implement a person class using templates or vector.

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: problem with push_back

    Quote Originally Posted by Fellow View Post

    Might appear strange, i know. I've just been searching for sites explaining how to implement a person class using templates or vector.
    Why? Person implies a single entity. vectors are used for collections. What are you trying to do?

  14. #14
    Join Date
    Dec 2011
    Posts
    11

    Re: problem with push_back

    I'd like to create with vector many persons:
    person1
    person2
    ...

    "people" could/should be the iterator, shouldn't it?

    In
    Code:
    for(int i=0; i<people.size(); i++)
    	{
    		cout<<(*people[i]).get_name();
    	}
    I thought "people" as an iterator is already working!?

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem with push_back

    Quote Originally Posted by Fellow View Post
    neither do I.
    I took the code from any site and tried to grasp what vector is doing.
    http://www.cplusplus.com/reference/stl/vector/
    I've just been searching for sites explaining how to implement a person class using templates or vector.
    What do you mean by searching for sites to implement a person class??? Are there sites dedicated in implementing a Door class, or a Car class, or an Elephant class, or a Globe class, or a BubbleGum class or ..??? You can have an infinite number of ways to implement anything.

    You don't learn C++ by "searching sites" and cherry-picking code. Maybe that's why things are not making sense.

    Regards,

    Paul McKenzie

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