Hello guys, I need your help, please have a look at the following code I get error
as following: no match for operator *(operand types are'doubles and 'lists')
this is inherited class, header file(before operator* function it worked properly)
Your * operator of lists needs parameters of type "lists". answer1 is from type double!
Operators are just another way to express a function!
1 2 3 4 5 6 7 8 9 10
lists lists::operator*(lists& answer)
lists answer1 = 1;
lists answer2 = 2;
lists answer = answer1 + answer2;
means:
call function "+" of answer1 with parameter 2 and put result in answer
With the code I wrote, I was thinking that I will multiple double (in this case answer1=15) with the lists (k=5) and get the answer 75 but it does not really works in this way.
I dont really get it could you please explain me? you mean after operator*() I need another operator () in order to get the result? I dont really understand. and also why should I return pointer to the variable?
I will appreciate your help, I have modified code even like this
lists lists::(const double& answer){
lists k = first * answer;
return k;
}
but error now is that var is private variable in this context
lists lists::operator * (const lists & answer)
{ lists k = first * answer.first;
return k;
}
You have some other problems also:
lists answer = answer1 * k; //here is an arror as compiler points
You've overloaded the * operator where the right hand side (RHS) is another lists object, but that's not what you're doing here. What you're trying to do is multiply a double and a lists. For that, you need another overload:
1 2 3 4
lists lists::operator * (double rhs)
{ lists k = first * rhs;
return k;
}
Your cout is missing std::
cout << answer;
In your operator >> overload, you outputting an uninitialized pointer.
os << "(" << arg.var << ")";
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.