Hello all. I'm pretty new at this, but I have a simple question about constructor with a vector class (or stack/list in STL). I want to use a constructor with the vectorclass to assign values in this concrete example to both data and data2. With the following command in main:
v.push_back(1,2);
This doesn't work. I can only push_back with one argument. How do I use a two argument constructor properly? I found out that I can assign data2 with the following:
(v[0]).setData2(2);
But I was hoping this could be done with the two argument constructor with the vector. Hope you can help me
Code:
#include <iostream>
#include <vector>
using namespace std;
class MyClass
{
public:
MyClass(int theData);
MyClass(int theData1, int theData2);
int getData() const;
void setData(int theData);
int getData2() const;
void setData2(int theData);
private:
int data;
int data2;
};
MyClass::MyClass(int theData)
{
data = theData;
}
MyClass::MyClass(int theData1, int theData2)
{
data = theData1;
data2 = theData2;
}
int MyClass::getData() const
{
return data;
}
void MyClass::setData(int theData)
{
data = theData;
}
int MyClass::getData2() const
{
return data2;
}
void MyClass::setData2(int theData)
{
data2 = theData;
}
int main()
{
vector<MyClass> v;
v.push_back(1,2);
return 0;
}
Sorry about not using inline functions.