Hi,
I'm trying to write a class that uses a vector, but I can't get the right syntax in the Staff constructor. Can anyone help?

public:
Staff();
Staff (???????????);

private:
vector<Employee> members;

My mind-reading device isn't working today - what are you actually trying to do with the constructor? Construct a Staff object using an existing vector<Employee> ? Construct it using a single Employee object? Make a copy constructor..? Construct it using some other value..?

Bear in mind that the syntax of a constructor is exactly the same as the syntax for any other function.. (Although constructors can have an initialisation list) How would you accomplish what you need with a plain old function?

My mind-reading device isn't working today - what are you actually trying to do with the constructor? Construct a Staff object using an existing vector<Employee> ? Construct it using a single Employee object? Make a copy constructor..? Construct it using some other value..?

Bear in mind that the syntax of a constructor is exactly the same as the syntax for any other function.. (Although constructors can have an initialisation list) How would you accomplish what you need with a plain old function?

Wow! You have a mind-reading device. That's cool!

Employee is a class. I'm supposed to write another class, Staff, that has functions add(), find(), etc. and uses "vector<Employee> members" in private section of the definition.

Thanks for your help.

OK - you still haven't said anything about what you want your constructor to do, which, I believe is what your original question was about :)

if your vector<Employee> is your only data member, then that doesn't need initialising in order for you to add elements to it. The main purpose of a constructor is to initialise data members and sometimes obtain memory resources for them. It seems from what you've said, that you don't need to do either of those, so you can just make do with the default constructor.

I'm not trying to be vague. I'm new to programming and don't know all the ins and outs yet. A constructor constructs. That's all I know. As to your original reply, I'm pretty sure I don't want a copy constructor. I think I want to construct a Staff object using an existing vector<Employee>.
Thanks.

I'm not trying to be vague. I'm new to programming and don't know all the ins and outs yet. A constructor constructs. That's all I know. As to your original reply, I'm pretty sure I don't want a copy constructor. I think I want to construct a Staff object using an existing vector<Employee>.
Thanks.

When you write a class, C++ already provides you with an invisible default constructor - you don't need to do anything, you don't even need to type it in your code. the constructor will automatically be invoked when you create a staff object, eg,

Staff myStaffList;

Since the only member of your Staff class is a vector<Employee> - a member which doesn't need initialising in order to use it (the vector class itself will have its own constructor which will allow it to be used), you should settle for the default constructor, and concentrate on your member functions for now.

On the other hand, if you later add other values, for example, you might want a string data member to hold the name of your Staff list, you should use a constructor to assign a default value such as "Unnamed Staff List", or a constructor taking a std::string argument to let you specify your own name at the point of construction.
eg,

class Staff
{
    vector<Employee> members;
    std::string listName
public:
    Staff() : listName("Unnamed List");
    Staff(std::string s) : listName(s);
};

This above example uses the initialisation list to assign values (preferred method for constructors)

You could also do the initialisation in the constructor body

class Staff
{
    vector<Employee> members;
    std::string listName
public:
    Staff() 
    {
        listName = "Unnamed List";
    }
    Staff(std::string s)
    {
        listName = s;
    }
};

This way might be a bit easier for you to read, since the syntax is identical to that of any other member function - the end result is the same however.

Can a constructor take more than one argument? If so what's the syntax? Is it something like

Staff(std::string s, double x)

Is there a situation where that would be useful or is it unconventional?

Thanks.

Can a constructor take more than one argument? If so what's the syntax? Is it something like

Staff(std::string s, double x)

Yes, that's perfectly acceptable. there's no arbitrary limit to the number of arguments you can take - although it becomes somewhat unweildy if you provide too many IMHO. (if you've got 6 or more, your code is probably going to look quite ugly - there's almost certainly a better way)

Also, as with member functions, you may define the body of the constructor outside the class declaration, eg,

class Staff
{
   vector<Employee> members;
public:
   Staff(std::string, double);
   Staff(Employee);
};

Staff::Staff(std::string s, double x)
{
   // do something here
}

Staff::Staff(Employee e)
{
    members.push_back(e);
}

Is there a situation where that would be useful or is it unconventional?

Many situations - you only need to look through the STL to find dozens of examples. The std::vector class has 8 different constructors, some of which take 2, 3 or 4 parameters.

A good example with vectors would be to create a new container based on some elements of an existing container:

#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> numberList;
    numberList.push_back(5);
    numberList.push_back(7);
    numberList.push_back(19);

    std::vector<int>::iterator myIter;
    myIter = std::find(numberList.begin(), numberList.end(), 7);

    std::vector<int> newNumberList(numberList.begin(), myIter); 
}

The snippet above creates a vector of int's holding these numbers - 5, 7, and 19
then the find() algorithm searches for the number 7 in the vector.
when it finds 7, it records the position into myIter

newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by myIter.

This is done by a vector constructor which accepts 2 STL iterators as parameters.

Thanks for all the examples. That's really helpful. :)

newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by myIter.

Urgh, sorry about that typo - that should have been numberList.begin()

This question has already been answered. Start a new discussion instead.