CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52

    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

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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.

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    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

  4. #4
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    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.

  5. #5
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    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!!!

  6. #6
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    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.

  7. #7
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    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!!!

  8. #8
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    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.

  9. #9
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    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!!!

  10. #10
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    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.

  11. #11
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    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!!!

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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
  •  





Click Here to Expand Forum to Full Width

Featured