The error:
g++ -Wall -Wextra -ansi -pedantic Testr.cc classE.cc classC.cc
classE.cc:46:14: error: expected unqualified-id before '&'token
classC.cc:46:14: error: expected unqualified-id before '&'token
The line the compiler is referring to:
classE classE::&operator%(classB &rhs){
The compiler is talking about the '&' in the 'classE::&operator%' expression; I'd just as soon delete the ampersand, but the assignment (yay arbitrary homework guidelines) requires it. I'm not even sure what the ampersand is supposed to do. Similar operator overloading methods use the same outline with no problem, and I have come up empty-handed through google and textbooks.
Specifying the derived class (classB.h is pure virtual) in the arguments doesn't clear these errors, either.
Because other similarly-defined operators work with no issue, I believe the problem is in the implementation. The basic idea is to take two classB-derived objects (classC or classE), and interleave the string variable each object contains with the other, returning a lhs-type object containing the interwoven string. The classE implementation is effectively exactly like the classC implementation:
classC classC::&operator%(classB &rhs){ //Interleave two classB-derived objects, return object of lhs-type
string temp = rhs.str(), temp2 = restore(), result = "";
int a = temp.size(), b = temp2.size(), max;
if (temp2.size() > temp.size()){
max = temp2;
}
else{
max = temp;
}
for (int i = 0; i < max; i++){
if (i < a){
result += temp2[i];
}
if (i < b){
result += temp[i];
}
}
return classC(result);
}
Sorry, I'm brand new here; I have no idea how to add line numbers to the code blocks =-\
Thanks for reading and considering my problem. =-)
EDIT: I was afraid it would be an embarrassingly stupid mistake; changing the line to classE& classE::operator%(classB &rhs){ has opened up the floodgates for tons of new errors. Heh, just when I thought I was getting the hang of C++. Thanks a bunch, guys. I hope all of my future questions are as simple.
classE classE::&operator%(classB &rhs)
Is not syntactically valid.
If you are overloading the %
operator all you need is:
classE classE::operator%(classB &rhs)
If your intention is to return a reference, you need:
classE& classE::operator%(classB &rhs)
^ ^ ^ ^ ^ ^
| | | | | |
| | | | | |
return type | | keyword| function arguments
| | |
class name | operator being overloaded
|
scope resolution operator