|
-
January 27th, 2012, 05:35 AM
#1
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
-
January 27th, 2012, 06:11 AM
#2
Re: problem with push_back
 Originally Posted by Fellow
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
-
January 27th, 2012, 06:31 AM
#3
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?
-
January 27th, 2012, 08:28 AM
#4
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*.
-
January 27th, 2012, 08:33 AM
#5
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.
-
January 27th, 2012, 08:34 AM
#6
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.
-
January 27th, 2012, 08:36 AM
#7
Re: problem with push_back
 Originally Posted by GCDEF
I'd wonder why the person class has a vector of type person called people.
Perhaps the person is pregnant. With twins.
-
January 27th, 2012, 11:04 AM
#8
Re: problem with push_back
-
January 27th, 2012, 11:15 AM
#9
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
-
January 27th, 2012, 11:19 AM
#10
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?
-
January 27th, 2012, 11:27 AM
#11
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.
-
January 27th, 2012, 12:03 PM
#12
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.
-
January 27th, 2012, 12:17 PM
#13
Re: problem with push_back
 Originally Posted by Fellow
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?
-
January 27th, 2012, 12:26 PM
#14
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!?
-
January 27th, 2012, 12:28 PM
#15
Re: problem with push_back
 Originally Posted by Fellow
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|