LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compile error when using copy operator (https://www.linuxquestions.org/questions/programming-9/compile-error-when-using-copy-operator-4175533357/)

allagar 02-07-2015 11:57 AM

compile error when using copy operator
 
Any idea why I should be getting this error?

I create 2 objects of the same class and then try to set one equal to the other. The class does have a overloaded copy operator?

TString temp1;
TString temp2;
temp1 = temp2;

error: no matching function for call to ‘TString::TString(TString)’
note: candidates are: TString::TString(TString&)
note: TString::TString(const char*)

const TString &TString::operator = (const TString &Src)
{
return Copy(Src);
}
arm-angstrom-linux-gnueabi-g++ (GCC) 4.2.4
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

neonsignal 02-07-2015 05:14 PM

Quote:

Originally Posted by allagar (Post 5313620)
error: no matching function for call to ‘TString::TString(TString)’
note: candidates are: TString::TString(TString&)
note: TString::TString(const char*)

Does the class have a constructor which takes no arguments (since your sample code does not initialize the strings)?

allagar 02-08-2015 10:28 AM

Yes it does have a default constructor taking no arguments:

TString::TString()
: TObj("String")
{
Len = 0;
MaxLen = 0;
Copy("");
}

NevemTeve 02-08-2015 10:59 AM

(Sorry wrong topic)

allagar 02-08-2015 11:28 AM

Something I have found is if I use the following compiler version I do no get the error. But now there are issues regarding shared libriaris (libc) on my target system which is using applications compiled with the orginal cross compiler version. Any suggestions if I should change the code to work with the old compiler or use the new compiler for the specific application and add the new libraries which I am not sure how to do this without causing compatibility issues on the target. Any help is appreciated.

arm-linux-gnueabi-g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ntubski 02-08-2015 05:37 PM

Quote:

Originally Posted by allagar (Post 5313620)
error: no matching function for call to ‘TString::TString(TString)’

This looks like it's trying to use a copy constructor. Perhaps some compiler versions manage to optimize the copy away.

a4z 02-09-2015 01:30 AM

pleas use code tags

this
Code:

const TString &TString:perator = (const TString &Src)
{
return Copy(Src);
}

is nonsense, the assignment operator has to return a reference to itself, aka return *this;


it is possible that this
Code:

TString temp1;
TString temp2;
temp1 = temp2;

becomes reordered and a constructor is called, especially if no useful code follows.
what optimization level did did you pass to the compiler? please provide complete information in your question.

however, if you think you need an assimgent operator you need also others

read this
http://en.wikipedia.org/wiki/Rule_of...programming%29

general, you do not implement your own string class, or if you do just for learning?


All times are GMT -5. The time now is 11:26 PM.