Friend operator<<

[EDITED]
I think I got the problem...blind me...



Hi, I'm writing a Polynomial class and having a compiler error on accessing private methods.


class declaration:
1
2
3
4
5
6
7
8
9
10
11
class Polynomial
{
   public:
     // ...
     friend std::ostream& operator<< (std::ostream&, const Polynomial&);

   private:
     unsigned int GetDegree() const { return itsCoeff.size()-1; }

     std::vector<double> itsCoeff;
};


implementation: operator<<
1
2
3
4
5
std::ostream& operator<< (std::ostream& output, const Polynomial& P)
{
   unsigned int theDegree = P.GetDegree();
   // blablabla...
}


and I get the error
In function 'std::ostream& operator<<(std::ostream, const Polynomial&)': 'unsigned int Polynomial::GetDegree() const' is private within this context


I've tried inlining the operator<< but the error remains.

I'm using Dev-C++.
Last edited on
why Do you use std::ostream ?. i think you use to namespace std; vs.
You forgot an '&' after std::ostream making the compiler think it was a different overload:
1
2
3
4
5
std::ostream& operator<< (std::ostream &output, const Polynomial& P)
{
   unsigned int theDegree = P.GetDegree();
   // blablabla...
}
i think you use to namespace std;

That's the same in terms of the final "effect".
using pollutes the scope of the header and forces users to be using that namespace, which I don't prefer.


Yeayea...very true Bazzy...I hate myself :P just realised it at the time you're typing...
Last edited on
Topic archived. No new replies allowed.