#### Archived

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

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

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

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
hmm, it may mean CStream isn''t defined yet. did you include the correct headers?

##### Share on other sites
Yep, all the headers are included, and it detects CStream as a class, just doesn''t copy it through a parameter.

##### Share on other sites
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 on other sites
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 on other sites
SpaceRogue - Thanks for the info. I''ve never had to write a copy constructor before, how would that be done?

##### Share on other sites
int Clowlev::Stream_SetID(CStream s)

you could try
int Clowlev::Stream_SetID(const CStream& s)

The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

##### Share on other sites
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 on other sites
Thanks for the help everyone, i''m working around it now, so it''s working.

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984427

• 25
• 11
• 10
• 16
• 14