am learning C++ coming from java world as a middle-range developer...
now am trying to learn how to do operator overloading
even tho I searched a lot in the web and SO:
How to properly overload the << operator for an ostream?
https://stackoverflow.com/a/476483/982161
this here is my reference:
the give this example:
// overload_date.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class Date
{
int mo, da, yr;
public:
Date(int m, int d, int y)
{
mo = m; da = d; yr = y;
}
friend ostream& operator<<(ostream& os, const Date& dt);
};
ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}
int main()
{
Date dt(5, 6, 92);
cout << dt;
}
But I want/need to develop this using the separation of headers and implementation
my Code
Header
#ifndef POINT_H
#define POINT_H
#include <ostream>
class Point
{
public:
Point();
virtual ~Point();
unsigned int Getx();
unsigned int Gety();
void Setx(unsigned int val);
void Sety(unsigned int val);
friend std::ostream& operator<<(std::ostream& ostream, const Point& dt);
private:
unsigned int x;
unsigned int y;
};
#endif // POINT_H
Implementation
#include "Point.h"
#include <ostream>
Point::Point()
{
x = 1u;
y = 1u;
}
Point::~Point()
{
//dtor
}
unsigned int Point::Getx()
{
return x;
}
unsigned int Point::Gety()
{
return y;
}
void Point::Setx(unsigned int val)
{
x = val;
}
void Point::Sety(unsigned int val)
{
y = val;
}
std::ostream& operator<<(std::ostream& out, const Point& a) {
return out << "A(" << a.Getx() << ", " << a.Gety() ")";
}
I can not compile teh code getting this error:
include\Point.h|24|error: passing 'const Point' as 'this' argument of 'unsigned int Point::Getx()' discards qualifiers [-fpermissive]|