Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error while using CList

Status
Not open for further replies.

raochetan

Programmer
Aug 20, 2001
62
IN
Can anyone tell me what is the problem in the following code. It gives me an error saying No Copy Constructor Available for MyStruct

In StdAfx.h:

struct MyStruct {
int x;
long y;
char z;
}

CList<MyStruct, MyStruct> MyList;


In the Main program:

MyStruct a;

a.x = 2;
a.y = 100000;
a.z= 'c';

MyList.AddTail( a );

How can one write a copy constructor for a structure.
 
The CList object needs this for its internal manipulation inside the AddTail method.

struct MyStruct {
int x;
long y;
char z;
MyStruct(MyStruct& ms)
{
x=ms.x;
y=ms.y;
z=ms.z;
}
MyStruct& operator=(MyStruct& ms)
{
x=ms.x;
y=ms.y;
z=ms.z;
return *this;
}
};

Hope this helps,
s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
That is really odd tho. It was my understanding that structs did not need a copy constructor. The reason for this was because a struct is only as large (sizeof) as its data members. I have tested this before by putting alot of functionality into a struct and if all i had in the struct for data was an int... the size of the struct would be 4.

I have never made a copy constructor for a struct and have always been able to:
MyStruct a,b;

a.x=1;
a.y = 2;
a.z = 3;

b = a; // this has never caused a problem.

Could someone post why we need a copy constructor with a struct. I am curious.

Matt
 
Yes Zyrentian, but you will not be able to:

MyList.AddTail( a );

Just &quot;step in&quot; in the debug mode in the AddTail event to see how it works and you will have a better grasp of the issue.

Hope this helps, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top