copy constructor and vectors

Started by
2 comments, last by Graham 18 years, 10 months ago
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.
Go on an Intense Rampage
Advertisement
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
CTemp(const CTemp &ctemp);

not:

CTemp(CTemp &ctemp);
Great that works, thanks for the extremely quick replies.
Go on an Intense Rampage

This topic is closed to new replies.

Advertisement