Archived

This topic is now archived and is closed to further replies.

dirkduck

"Cannot convert parameter 1 from 'A' to 'A'" ?

Recommended Posts

dirkduck    122
Hi everyone. I just added some code, and MSVC++ 6 is flagging up a couple errors. I made a new class ''CStream'' and am trying to pass a parameter ''CStream s'' into a function. Heres some code:
int Clowlev::Start()
{
     CStream st;
     int id=Stream_SetID(st);
...
}

...

int Clowlev::Stream_SetID(CStream s)
{
...
}
When I try to compile the code, MSVC gives me the error: error C2664: ''Stream_SetID'' : cannot convert parameter 1 from ''class CStream'' to ''class CStream'' No copy constructor available for class ''CStream'' I''ve been able to do something simmilar with other classes ive made just fine, but this one flag''s the error. Does anyone know what would cause this? Thanks.

Share this post


Link to post
Share on other sites
SpaceRogue    122
Your second function is passing a CStream object by value which requires a copy constructor. Normally the compiler will create a default copy constructor for you, but sometimes it can''t depending on the definition of the CStream class.

Share this post


Link to post
Share on other sites
dirkduck    122
Hi, thanks for the reply. I tried switching the parameter to CStream *s and calling it with the paremtere &st, but it gave me the same error. Is there another way to handle it?

Share this post


Link to post
Share on other sites
SpaceRogue    122
The exact same error? You rebuilt everything? Pass by value is one instance where the copy constuctor is used, but it isn't the only one.

Even if you fix it by passing a pointer, the problem of not being able to copy CStream objects remains...unless you will never do that. The real solution is to write a copy constructor. Just make sure you copy each member properly, e.g. pointers normally aren't copied since they usually should point to different things, even if the values they point to should be the same. The same goes for references.


Edit: Even if you never want to copy CStream objects, it makes sense to declare a copy constructor and make it private to show the intent of your design.

[edited by - SpaceRogue on August 5, 2003 10:10:46 PM]

Share this post


Link to post
Share on other sites
dirkduck    122
I was just messing around with it, and one of the members of CStream is ''fstream file''. Anyways, if I comment that member out, it works OK, so it must be something with copying over the fstream I assume? Is there a way to get past this? Thanks.

Share this post


Link to post
Share on other sites
SpaceRogue    122
Yes, fstream would be a problem.

Copy constructors are just like regular constructors except they take a single parameter of a constant reference to the class.

Example:

CStream(const CStream& source)
{
//Copy source members to current object
}

That's it. The trick is to make sure you copy your members properly and to make sure the ones that require initialization (like references, const members, or members that don't have a default constructor) are put in the initialization list and NOT in the function body. The reason is that members are actually constructed before the body begins. This means unless you initialize these particular members in the initialization list, they won't be initialized properly.

Example:

A copy constructor for a class containing a constant integer member called a:

MyClass(const MyClass& source)
{
a=source.a; //Wrong!
}

MyClass(const MyClass& source): a(source.a)
{
//Right! The constant is initialized to the proper value when constructed.
}


[edited by - SpaceRogue on August 5, 2003 10:37:33 PM]

Share this post


Link to post
Share on other sites