Sign in to follow this  
Graham

copy constructor and vectors

Recommended Posts

Graham    166
I get a compile error with this code
#include <iostream>
#include <vector>
using namespace std;

class CTemp
{
public:
	int a;
	CTemp(CTemp &rhs)
	{cout << "Copy cons" << endl;}
	CTemp()
	{cout << "cons" << endl;}
};

void function(vector<CTemp> &vec)
{
	CTemp temp1;
	vec.push_back(temp1);
	return;	
}

int main()
{
	cout << "in main" << endl;
};
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2558: class 'CTemp' : no copy constructor available or copy constructor is declared 'explicit' If I comment out the copy contructor then it compiles. So how can I use vectors of Classes that have custom copy constructers. I must be missing something fundemental here. Graham.

Share this post


Link to post
Share on other sites
SirKnight    316
The copy contructor is supposed to take a const reference. So change it to this:

CTemp( const CTemp &rhs )
{
blah
}

See this for more info: http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

EDIT: meh....

-SirKnight

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this