Two argument Vector<MyClass> Constructor

This is a discussion on Two argument Vector<MyClass> Constructor within the C++ Programming forums, part of the General Programming Boards category; Hello all. I'm pretty new at this, but I have a simple question about constructor with a vector class (or ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Two argument Vector<MyClass> Constructor

    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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by Cozzie View Post
    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.
    Right, well remember that push_back expects an object - it doesn't forward the arguments to the constructor. So what you want is basically:

    Code:
    int main()
    {
        vector<MyClass> v;
        v.push_back(MyClass(1,2));
        return 0;
    }



    ITSA
    Socket Library!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default constructor == constructor without argument?
    By cyberfish in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2009, 03:25 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Assignment operator in a constructor argument
    By chadwickstein in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2006, 04:08 PM
  4. calling default instead of argument constructor, why?!
    By cppn00b in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2005, 03:24 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21