Why? eval.cpp:25: error: no match for 'operator+' in '*result_plus + *car(iter)'

H

hopangtsun

Hello all,

I've got a problem on operator overloading, I don't know what's wrong
with my code.
Can anyone kindly tell me what's the problem is?
Thanks

//this is the error message I've got
eval.cpp:25: error: no match for 'operator+' in '*result_plus +
*car(iter)'

//here is my code
//Cell.cpp overload operator+
Cell* operator+ (const Cell &a, const Cell &b) const {
if (a.is_cons()){
eval(&a);
}
if (b.is_cons()){
eval(&b);
}
if (a.is_int() && b.is_int()){
int result_i = a.get_int() + b.get_int();
return new IntCell(result_i);
}
if (a.is_int() && b.is_double()){
double result_d = a.get_int() + b.get_double();
return new DoubleCell(result_d);
}
if (a.is_double() && b.is_int()){
double result_d = a.get_double() + b.get_int();
return new DoubleCell(result_d);
}
if (a.is_double() && b.is_double()){
double result_d = a.get_double() + b.get_double();
return new DoubleCell(result_d);
}
if (a.is_string() && b.is_string()){
string result_s = a.get_string() + b.get_string();
const char* result_c = result_s.c_str();
return new StringCell(result_c);
}
else {
cout << "Error: the expression is illegal" <<endl;
exit(0);
}
}

//main.cpp
Cell* result_plus = make_int(0);
iter = cdr(iter);
while (iter !=NULL){
result_plus = *result_plus +
*car(iter);
iter = cdr(iter);
}
return result_plus;
 
V

Victor Bazarov

I've got a problem on operator overloading, I don't know what's wrong
with my code.
Can anyone kindly tell me what's the problem is?

Not enough information.
//this is the error message I've got
eval.cpp:25: error: no match for 'operator+' in '*result_plus +
*car(iter)'
[...]
//main.cpp
Cell* result_plus = make_int(0);
iter = cdr(iter);
while (iter !=NULL){
result_plus = *result_plus +
*car(iter);
iter = cdr(iter);
}
return result_plus;

What does "car(iter)" evalutate to?

V
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello all,

I've got a problem on operator overloading, I don't know what's wrong
with my code.
Can anyone kindly tell me what's the problem is?
Thanks

//this is the error message I've got
eval.cpp:25: error: no match for 'operator+' in '*result_plus +
*car(iter)'

//here is my code
//Cell.cpp overload operator+
Cell* operator+ (const Cell &a, const Cell &b) const {

Try this program:

struct Cell
{
Cell* operator+ (const Cell &a, const Cell &b) const
{
return this;
}
};

int main(){}

If your compiler doesn't tell you directly what's wrong, then you should
be able to figure it out nonetheless.

I mean, this is a pretty strong hint. ;-)
 
V

Victor Bazarov

car(iter) will return the Cell pointer of the its car

sorry that I may not explain it in details
but the following links will have better explaination
http://en.wikipedia.org/wiki/Cons
http://en.wikipedia.org/wiki/Car_and_cdr

so what I am try to do is
there are 3 cells called a, b and c
and I can use the "+" in this way
a = b + c;

What book do you use to learn about operator overloading? What's
it say about operators as member functions versus non-members?

V
 
H

hopangtsun

I use Thinking in C++ as reference book
but it said there are no differences between member function and
non-members operator overloading
 
I

Ian Collins

I use Thinking in C++ as reference book
but it said there are no differences between member function and
non-members operator overloading
Please don't top-post.

Victor suggested you look up operators as member functions versus
non-members, as well as overloading, two different topics.

Your book should explain when it is and isn't appropriate to use either
form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top