Jump to content
  • Advertisement
Sign in to follow this  
Graham

copy constructor and vectors

This topic is 4750 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
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!