Overloaded Operators

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)

class lists:public vectorebi
{
public:
lists (double first_);
lists (double first_, lists var_);
double operator-(const double& answer);
lists operator*(lists& answer);
virtual ~lists(){};
private:
friend std::ostream& operator<<(std::ostream& os, const lists& arg);
double first;
lists* var;
};

////source file

lists::lists(double first_){
first=first_;
}
lists::lists(double first_, lists var_){
first=first_;
var=&var_;

}
double lists::operator-(const double& answer){
//vector<double> myVec= {1.0,2.1,3.5}
double result = answer - first;
return result;
}

lists lists::operator*(lists& answer){
lists k = first * answer;
return k;
}

std::ostream& operator<<(std::ostream& os, lists& arg) {

os << "(" << arg.var << ")";
return os;

}

and the main ///
int main(){
double answer1 = 15;
lists k=5;
lists answer = answer1 * k; //here is an arror as compiler points

cout << answer;

}

I will appreciate your help, I am trying to multiply variable of type my class and double, is it possible?

looking forward for your answers )))
Last edited on
closed account (48bpfSEw)
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


closed account (48bpfSEw)
Oh I oversaw your question! ^^

Yes it is possible to mulitply lists with double. You need a cast-operator which casts double in lists.
Could you please tell me what is the problem? if I change it in a way you wrote, it will be sum of lists variables but I need lists and double.
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.
closed account (48bpfSEw)
you need something like that in your class:

operator double() { return this->first; }


http://www.learncpp.com/cpp-tutorial/910-overloading-typecasts/

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
You have the right idea overloading the * operator, but as you've written it, the operator is recursive.

1
2
3
4
lists lists::operator * (lists & answer)
{   lists k = first * answer;  // Invokes operator * recursively
    return k;
}


Should be:
1
2
3
4
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.


Topic archived. No new replies allowed.