-
September 26th, 2003, 03:32 AM
#1
returning a vector object
Hello,
I'm stuck on a program where I'm supposed to write a member function that returns a vector!!
let say my code looks like the following:
Code:
template<typename TYPE>
class Employees
{
public:
void addEmployees();
private:
vector <TYPE> worker;
string city;
int age;
int count;
};
now what I need to do is write a constructor that takes the name of the city and age as arguments and then returns a set of objects of type TYPE with count = 0 .Something like makeEmployees(city, age).
Thank you very much.
Sofia
-
September 26th, 2003, 05:12 AM
#2
Firstly, a constructor can't return a value.
I'm not sure if I understand what you're asking, but if you want a function to return the member vector, it is no different than returning an int if I'm not mistaken. ("return worker;")
PS: If the "count" variable is to keep track of the number of workers in the class, use "worker.size()" instead. The vector class does the work for you.
-
September 26th, 2003, 09:35 AM
#3
Correct.
There are several solutions. One solution is a pointer to the constructor, thus it updates the data within its boundary. Another solution is a member function that returns the data. Another solution is messages.
Kuphryn
-
September 27th, 2003, 12:04 AM
#4
Originally posted by Assmaster
Firstly, a constructor can't return a value.
First of all thanks for the reply.
For some reason, I always thought that a constructor does return an object eventhough implicitly.
Well back to my question. What I am stuck on is how to initilize the vector. If there wasn't a vector there I would have done something like(I'm not sure if I'm allowed to make a default constructor take an argument though):
Code:
Employees::Employees(string city) :city(city), age(0), count(0) {}
but with the vector of type TYPE, I have no idea how to go about it.
Any help would be very appreciated.
Thanks
Sofia.
-
September 27th, 2003, 12:21 AM
#5
You do not need count if its purpose is to keep track of the size
of the vector.
You do not need to "initialize the vector" unless you actually want
it to hold a real object of TYPE.
A default constructor can not take arguements.
What exactly are you trying to do?
DEFENDER OF ALL THINGS STL!!!
-
September 27th, 2003, 12:29 AM
#6
Count is meant to keep track of the number of cities not the size of the vector.
I need to initilize the vector as it's of type TYPE hence it might take at some point a type that doesn't have a default constructor.
Now all I wanted to do is define a constructor that would initilize all the members of my class templates as the following(city=city,age=0,count=0and an empty vector).
Thanks
Sofia.
Last edited by megabyte; September 27th, 2003 at 02:06 AM.
-
September 27th, 2003, 12:35 AM
#7
This is done when the template class is instantiated
Employees<WHATEVER> SomeEmployees("Miami");
SomeEmployess will now have a member which is a vector
which holds WHATEVER. It is empty.
You have to make sure that Whatever assignable and copyable.
DEFENDER OF ALL THINGS STL!!!
-
September 27th, 2003, 12:45 AM
#8
Originally posted by STL MAN
This is done when the template class is instantiated
Employees<WHATEVER> SomeEmployees("Miami");
SomeEmployess will now have a member which is a vector
which holds WHATEVER. It is empty.
You have to make sure that Whatever assignable and copyable.
Sorry but I don't really get what you mean especially with the last line.
Edit:As a side note, can anybody please explain to me what this line would do.
Code:
sort(worker.begin(), worker.end(), compare);
Last edited by megabyte; September 27th, 2003 at 01:59 AM.
-
September 27th, 2003, 01:56 AM
#9
Now you have me confused?
You are defining a template class called Employees. When
you declare an instance of this class you specify what the template
parameter is. In this case this template parameters is being used
to specify what is held by the vector worker. Therefore whatever
TYPE is, it must be copyable, assignable, destroyable with a
destructor and for function like std::vector resize() it must have
a default constructor which is public. Now I don't what you mean
intitailize TYPE. This is not the responsibility of the vector and as
you have written your class Employees, not its responsibility either.
What I am saying is that when you declare an instance of your template class
a empty vector which holds the type you specify will be created.
You do not have to worry about it.
DEFENDER OF ALL THINGS STL!!!
-
September 27th, 2003, 02:09 AM
#10
It doesn't really matter whether I'm supposed to worry about it or not. What I wanted is to learn how I can define a constructor that would initilize my class members.
Thanks for the reply anyways.
Sofia.
-
September 27th, 2003, 02:16 AM
#11
Well it is clear you don't know what a template class is and you
don't know what the particular template class vector is. I think
you need to do some research before you use them.
I will repeat, If your goal from your constructor is to have an instantiation of the template which has a member which is
an empty vector holding whatever you specified as the template
parameter, then you do not have to do anything in the constructor
with respect to the vector.
DEFENDER OF ALL THINGS STL!!!
-
September 27th, 2003, 08:40 AM
#12
Originally posted by megabyte
Edit:As a side note, can anybody please explain to me what this line would do.
Code:
sort(worker.begin(), worker.end(), compare);
It will sort the complete container 'worker' using the method 'compare'...
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
|