Thread: std::vector resize and Class copy constructor

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    std::vector resize and Class copy constructor

    Code:
    #include <vector>
    
    class T1
    {
    public:
    	T1(int* a) : m_pInt(a) {}
    	T1(T1& a) : m_pInt(a.m_pInt) {}
    private:
    	int* m_pInt;
    };
    
    int main(void)
    {
    	std::vector<T1> test;
    	T1 t(NULL);
    	test.resize(10,t);
    	return 0;
    }
    I see no reason why this shouldn't compile. Of course, when the compiler and I are in disagreement, the compiler wins. Here's the error I get:
    Code:
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810) :
        error C2558: class 'T1' : no copy constructor available or copy constructor
        is declared 'explicit'
            C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(809) :
                while compiling class-template member function
                'void std::vector<_Ty>::_Insert_n(std::vector<_Ty>::iterator,
                    std::vector<_Ty>::size_type,const _Ty &)'
            with
            [
                _Ty=T1
            ]
            main.cpp(20) : see reference to class template instantiation 'std::vector<_Ty>'
                being compiled
            with
            [
                _Ty=T1
            ]
    As you can see, I'm using .Net 2003.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    T1(const T1& a) : m_pInt(a.m_pInt) {}

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Yup...just figured that out. Thanks.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The Rule of Three states that if you have to implement the copy constructor, destructor, or assignment operator, you ought to implement all three.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM