The code below cannot be compiled. However, when I remove "const" from Point& of the friend function, this code turns to be compiled. Could anyone explain the reason why?
class Point
{
public:
Point(double x, double y);
Point operator+(const double& add);
friend Point operator+(const double& add, const Point& p){return p+add;}
private:
double px, py;
};
Point::Point(double x, double y): px(x), py(y){}
Point Point::operator+(const double& add){
return(Point(px+add, py+add));
}
int main(){}
The operator+ is not marked as a const, yet is tried to be called through a const reference.
Constant pointers and references are only allowed to call member functions that are marked constant (since the compiler definitely knows that those functions are ensured not to modify internal state).
Related
I have been encountering an issue where I overloaded the [] operator and I am running into issues when compiling my code.
The overload defintion
double& Point::operator[](int index) { return index ? y : x; }
The class Point
`class Point {
public:
// declare constructors (3)
Point();
Point(double * arr);
//Point(double x_input, double y_input);
Point(const Point& input);
Point(const double x_input, const double y_input);
// declare overloaded operators (8 member functions)
double& operator[](int index);
Point operator+=(double num);
Point operator+=(const Point& pointee);
Point operator++(int);
Point operator++();
Point operator--(int);
Point operator--();
private:
double x; // The x-coordinate of a Point
double y; // The y-coordinate of a Point
};`
The error:
error
I am not sure as to why I am encountering this issue and I was hoping if someone can shed some light as to what is happening. Thank you in advance.
I have tried adding const to the overload and its return type but to no avail.
The error message means that the operator [] is called for a constant object of the type Point but the operator is not declared as a constant member function.
Add one more overload to the operator
// declare overloaded operators (8 member functions)
double& operator[](int index);
const double& operator[](int index) const;
If the operator is defined in the global namespace outside the class definition then you need to write
const double& hlp2::Point::operator[](int index) const
{
//,,,
}
If the operator is defined in the same namespace where the class is defined then write
const double& Point::operator[](int index) const
{
//,,,
}
Shortly speaking define it as you defined the non-constant operator.
I am stuck with regards to the 2 non-member, 2 non-friend multiplication and the addition of the operator overloading functions. I am unsure of how to do it. Could someone please assist in helping me to resolve this? Refer to my codes below. Thank you in advance!
Compiler output:
Point.cpp:208:19: error: passing ‘const CS170::Point’ as ‘this’ argument discards qualifiers [-fpermissive]
return other + value;
^~~~~
Point.cpp: In function ‘CS170::Point CS170::
operator*(double, const CS170::Point&)’:
Point.cpp:215:10: error: ‘double CS170::Point::x’ is private within this context
result.x = value * x;
^
Point.cpp:215:22: error: ‘x’ was not declared in this scope
result.x = value * x;
^
Point.cpp:216:10: error: ‘double CS170::Point::y’ is private within this context
result.y = value * y;
^
Point.cpp:216:23: error: ‘y’ was not declared in this scope
result.y = value * y;
Point.h
#include <iostream> // istream, ostream
namespace CS1100
{
class Point
{
public:
// Point(double X, double Y); // Constructors (2)
explicit Point(double x, double y);
Point();
Point operator+(const Point& other)const ;
Point& operator+(double value);
Point operator*(double value) ;
Point operator%(double value);
Point operator-(const Point& other)const ;
Point operator-(double value);
Point operator^(const Point& other);
Point operator+=(double value);
Point& operator+=(const Point& other) ;
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
Point& operator-();
// Overloaded operators (14 member functions)
friend std::ostream &operator<<( std::ostream &output, const Point &point );
friend std::istream &operator>>( std::istream &input, Point &point );
// Overloaded operators (2 friend functions)
private:
double x; // The x-coordinate of a Point
double y; // The y-coordinate of a Point
// Helper functions
double DegreesToRadians(double degrees) const;
double RadiansToDegrees(double radians) const;
};
// Point& Add(const Point& other); // Overloaded operators (2 non-member, non-friend functions)
// Point& Multiply(const Point& other);
Point operator+( double value, const Point& other );
Point operator-( double value, const Point& other );
My source code:
///////////////////////////////////////////////////////////////////////////////
// 2 non-members, non-friends (operators)
double operator+( double value, const Point& other )
{
return other + value;
}
double operator*( double value, const Point& other )
{
Point result;
result.x = value * x;
result.y = value * y;
return result;
}
As far as I understand the discussion to the question, the problem is not really the operators themselves, but the number of allowed member functions being limited – and you already have exceeded this limit.
However, you have quite a number of functions that don't need to be members, for instance:
class Point
{
public:
Point operator+(const Point& other) const
{
return Point(x + other.x, y + other.y);
}
};
Make free functions from all these:
class Point { /*...*/ };
Point operator+(Point const& l, Point const& r)
{
return Point(l.getX() + r.getX(), l.getY() + r.getY());
}
Having moved out all these operators like the one shown above, you get away far enough from the limit so that you can introduce the needed getters:
class Point
{
public:
double getX() { return x; };
double getY() { return y; };
};
If you are willing to rename the member variables, e. g. by adding a prefix, you can follow another pattern:
class Point
{
double m_x, m_y;
public:
double x() { return m_x; };
double y() { return m_y; };
void x(double v) { m_x = v; }; // the corresponding setter
// (for illustration, you might not need it)
};
This latter pattern is quite common, too. Advantage is being shorter for skipping the explicit get or set prefix, disadvantage is exactly losing this explicitness... Decide you, which one you prefer. More important than personal preference is consistency, though, so if there's e. g. a company's convention or common practice, follow that one...
Some of your operators will need to remain members, though, these are all those that modify the current object:
class Point
{
public:
Point& operator+=(const Point& other) /* const */ // NEEDS to be non-const
{
x += other.x;
y += other.y;
return *this; // <- very good hint to spot the ones needing to stay members
}
};
If you have a public copy constructor, you can re-use the operator+= for defining the operator+:
class Point
{
public:
Point(Point const& other) : Point(other.x, other.y) { }
};
Point operator+(Point const& x, Point const& y)
{
Point r(x); // or Point(x.x(), x.y()), if you lack such constructor)
r += y;
return r;
}
Actually, you can even spare the explicit copy by accepting one of the parameters by value:
Point operator+(Point x, Point const& y)
// ^ no reference
{
return x += y;
}
The latter rather for illustration, I'd prefer the two references in given case to keep the symmetry of the interface...
Suppose I have the following class:
class Point{
private:
int x,y;
public:
int get_x() const {return x;}
int get_y() const {return y;}
Point() :x(0),y(0){}
Point(int x,int y):x(x),y(y){}
Point(const Point& P){
x = P.get_x();
y = P.get_y();
}
Point& operator= (const Point& P) {
x = P.get_x();
y = P.get_y();
return *this;
}
friend ostream& operator<<(ostream& os,const Point& P) {
os<<"["<<P.get_x()<<", "<<P.get_y()<<"]";
return os;
}
Point operator - (const Point &P){
return Point(x-P.get_x(),y-P.get_y());
}
friend bool operator > (const Point &A, const Point &B) {
return A.get_y()>B.get_y();
}
};
Here I used friend function. I can also use function without friend:
class Point{
...
bool operator > (const Point &B) const {
return y>B.get_y();
}
...
};
What are the differences between them in actual implementations? Also in the second method, the code won't compile without 'cont', why is that? Even after I changed the getter function into non-const function, it still won't compile without the 'const'.
As you've already noticed, comparison operator overloads can either be implemented as a member function or as a non-member function.
As a rule of thumb you should implement them as a non-member non-friend function where possible, as this increases encapsulation, and it allows (non-explicit) conversion constructors to be used on either side of the operator.
Say for instance your Point class for whatever reason had an int conversion constructor:
Point(int x);
With a non-member comparison operator you can now do the following:
Point p;
p < 3; // this will work with both a member and non-member comparison
3 < p; // this will **only** work if the comparison is a non-member function
You also seem to be confused about when to use const, again as a rule of thumb for comparison operators you should always use const wherever possible, because comparisons logically do not involve any change to the object.
As Point is a very small class you could also take it by value instead, so in order of most to least preferable your options are:
// Non-member, non-friend
bool operator>(Point const& A, Point const& B);
bool operator>(Point A, Point B);
// Non-member, friend
friend bool operator>(Point const& A, Point const& B);
friend bool operator>(Point A, Point B);
// Member
bool Point::operator>(Point const& B) const;
bool Point::operator>(Point B) const;
I can't figure this out. I'm trying to recreate "complex" c++ class and those functions.
For example: real(), imag(), abs() etc. use parameters as const complex& obj. So I thought if you pass object by its reference it is not copied and saves a bit of memory and is faster and use const in front of it to avoid messing up that object. Am I right?
So I thought I could create my functions like those as well, but why those functions that have parameter with const need to be const functions them selves?
Here is what I got:
class MyComplex{
private:
double real;
double imag;
public:
MyComplex();
MyComplex(int real);
MyComplex(int real, int imag);
double getReal();
double getImag();
void setReal(int real);
void setImag(int imag);
friend const double real(const MyComplex& obj);
friend const double imag(const MyComplex& obj);
friend const double abs(const MyComplex& obj);
friend const double arg(const MyComplex& obj);
friend const double norm(const MyComplex& obj);
friend const std::string conj(const MyComplex& obj);
friend std::string myPolar(double rho, double theta);
};
If I don't put const in front of them they don't work.
const double real(const MyComplex& obj){
return obj.real;
}
Other functions are very similar.
Your design is bad.
What you should change:
getReal and getImag should be const-member-functions (Add the qualifier after the parameter-list).
All those friend-functions should not be so friendly, implement them just with the public interface.
(Remember that encapsulation means minimizing the amount of code depending on the classes internal details.)
Anyway, there is seldom a reason to have a const-qualified return-type. I never had one.
Making the return-type const might even sometimes break things.
The declaration
friend const double real(const MyComplex& obj);
does not declare a const function, it declares a non-const function that returns a constant value. Since you return by value anyway, this const is plain useless.
A const function is declared like this:
//within the class definition
double getReal() const;
This makes the implicit parameter to getReal(), the this pointer, const, allowing you to do myConstComplex.getReal().
And, as Deduplicator correctly notes, just kick those friends, you don't need them and you don't want them.
There's no reason to return const built-in type or object.
I'm new to object oriented programming and I'm wondering if it's possibile to create an operator for one class which will use arguments from this class and another one, declared by me.
The problem I have to solve is a linear translation on a given point. So I created classes Point and LinearTranslation and basically what I want to do is to create an operator
Point operator* (Point p, LinearTranslation l)
which would take Point p, do the translation l and then return Point. I'm getting weird errors though: Point operator*(int)’ must have an argument of class or enumerated type or that LinearTranslation has not been declared.
Is it even possibile?
ok i'm posting only a bit because it's kind of assignment so I have class Point.h
sorry for the mess but I'm trying a bit everything and it's not working for me.
#include "LinearTranslation.h"
class Point {
public:
int N;
double* coordinates;
public:
Point(int, double*);
virtual ~Point();
double operator[](int a);
//friend Point operator*(LinearTranslation l);
friend Point translation(LinearTranslation l);
};
LinearTranslation.h
#include "Point.h"
class LinearTranslation {
private:
int N;
double* vector;
double** matrix;
public:
//friend class Point;
LTrans(int,double*,double**);
LTrans(int);
virtual ~LTrans();
void write_vector();
void write_matrix();
LinearTranslation operator+(const LinearTranslation&);
friend Point& translation(LTrans l, Point p);
//friend Point& operator* (Point p, LTrans l);
};
in your class declaration:
//friend Point operator*(LinearTranslation l);
a friend operator overloading function takes two parameters for the function, you only declare one in your code. The most common example of a friend function is using the overloaded operator << like so:
friend ostream& operator<<(ostream& output, const Point& p);
note that this function takes the output as its first parameter, and the point as its second parameter.
Your fix would be to add in your operand to the function
friend Point operator*(LinearTranslation l, const int& MULT);