Re: problem with push_back
Quote:
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.
Quote:
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*>]
Why are you trying to add a string to a vector that takes a person pointer?
Regards,
Paul McKenzie
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*.
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.
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.
Re: problem with push_back
Quote:
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.
Re: problem with push_back
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
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?
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.
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.
Re: problem with push_back
Quote:
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?
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!?
Re: problem with push_back
Quote:
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/
Quote:
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
Re: problem with push_back
Quote:
Originally Posted by
Fellow
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!?
people isn't an iterator, it's a vector. That code doesn't directly use iterators.
Re: problem with push_back
Quote:
Originally Posted by
Fellow
I'd like to create with vector many persons:
person1
person2
Code:
#include <vector>
#include <string>
class Person
{
int age;
int weight;
int gender;
int height_in_inches;
std::string name;
std::string country_of_birth;
//...
//.. add public members
};
typedef std::vector<Person> PersonVector;
int main()
{
PersonVector pV;
pV.push_back( Person() );
}
The Person class describes a single person. The PersonVector is an alias for a vector of Person.
So what do you not understand about the code above? You should start with something more coherent such as the code above.
Regards,
Paul McKenzie
Re: problem with push_back
Quote:
Originally Posted by
Fellow
I'm not getting to the core of all the matter.
Whats so wrong about a little 'reverse engineering' ?
What exactly are you "reverse engineering"?
You don't learn C++ this way -- that's the cold, hard facts. Take it from me, a person who has taught C++.
Regards,
Paul McKenzie
Re: problem with push_back
Quote:
Originally Posted by
Fellow
Whats so wrong about a little 'reverse engineering' ?
Well, for one thing it doesn't appear to be working very well so far.
Re: problem with push_back
Quote:
Originally Posted by
Fellow
I'm not getting to the core of all the matter.
Whats so wrong about a little 'reverse engineering' ?
What's wrong is you're looking at things you don't understand and asking questions that don't make any sense. If you want to learn the language, and there's a very steep learning curve, get a good book that takes you through the basics in a well organized, methodical manner. You'll never learn C++ by looking at code and trying to guess what it does.
Re: problem with push_back
I'm sorry.
It was not my intention to be impolite or bumptious.
I just wondered what some people could tell me if I'd ask such questions.
I appreciate your criticism!
Re: problem with push_back
GCDEF, I agree with you.
I want to make headway by different approaches. And I'm really trying to code my own stuff.
Re: problem with push_back
Quote:
Originally Posted by
Fellow
I'm sorry.
It was not my intention to be impolite or bumptious.
I just wondered what some people could tell me if I'd ask such questions.
I appreciate your criticism!
Nobody said you were impolite, just embarking on a journey that would prove frustrating and fruitless.
Re: problem with push_back
Many thanks for the example, Paul McKenzie!
Re: problem with push_back
Quote:
Originally Posted by
Fellow
I'm sorry.
It was not my intention to be impolite or bumptious.
You were not impolite.
It's that the C++ language isn't something that can be learned informally without structure. Some try but then get tangled in all sorts of knots trying to write the simplest of programs.
Others try to write advanced programs (for example using templates), but are lacking in the basic C++ fundamentals required to write such code (the "cherry-pickers" suffer from this fate).
A good book or set of books should be used, so that you understand each concept before moving on to another concept.
Regards,
Paul McKenzie