Sign in to follow this  

copy constructor and vectors

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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