Reputation: 384
I try to overload operator in C++, but I am having some difficulties. This is what I have in my class:
ostream & operator <<(ostream & s)
{
s << w();
return s;
}
string w()
{
stringstream ss;
string str;
for (int i=n-1; i>=0; i--)
{
if (i==n-1)
{
ss<<tablica[i] << "x^" << i;
}
else
{
if (tablica[i]<eps && tablica[i]>-eps) ss <<"+" << +tablica[i]<< "x^" << i;
else if (tablica[i]<eps)ss << tablica[i]<< "x^" << i;
if(tablica[i]>eps) ss <<"+" << +tablica[i]<< "x^" << i;
}
}
ss >> str;
return str;
}
I am trying to use this like this:
cout << p << endl;
Error: no match for 'operator<<' in 'std::cout << p
Here is my whole code of program: http://codepad.org/xBijPMCp
Upvotes: 0
Views: 1527
Reputation: 11
Operator overloading is the ability to tell the compiler how to perform a certain operation when its corresponding operator is used on one or more variables.
For example, the compiler acts differently with regards to the subtraction operator -
depending on how the operator is being used.
-
symbol is doubled up and placed on one side of a variable, such as --Variable
or Variable--
, the value of the variable needs to be decremented; in other words, the value 1 shall be subtracted from it.All of these operations work because the subtraction operator -
has been reconfigured in various classes to act appropriately.
Upvotes: 1
Reputation: 3571
You first need to think about your general design.
I think you want to overload <<
to work with the type of your variable tablica
. The operator<<()
is a binary operator: the declaration have to be of the form:
<<(Type t1, Type t2).
(is you want it to be a member this will "degenarte" to only one parametr, because the first-the left, will be a kind of this
). In your case something like:
ostream & operator <<(ostream & s, const tablica_type &c);
Your tablica is a global variable, and n too, with normaly is not a good idea. You will probably want to define a class woth hold the tablica and the n, and for this class override <<().
Upvotes: 0
Reputation: 490048
An overloaded operator is invoked in one of two ways. Either as a member function, where x op y;
is treated as x.op(y);
or else a global, where x op y;
becomes op(x,y);
.
Note that when the operator is a member function, the left operand must be the one for which you overload the operator. In the case of inserting into a stream: x << y;
, the left operand is the stream object, so to overload the operator as a member function, you'd have to do the overload as a member of the stream class.
Since modifying the stream classes is pretty much off-limits, your only real choice is to implement the overload as a global function. In this case, however, the function must take two parameters (one for the left operand, one for the right operand).
Therefore, an insertion operator nearly always needs a signature like:
std::ostream &operator<<(std::ostream &os, T const &t)
(where T
is whatever type you're going to insert).
Upvotes: 2
Reputation: 4025
ostream & operator <<(ostream & s)
should be implemented in the relation to a particular class(to make it usefull), so signature should be
friend ostream & operator <<(ostream & s, const class_name &c);
Upvotes: 4