Access member variables/getters from comparison function - c++

I'm basically trying to write comparator function to sort by x then y value of Object P1 and Pt2 in both ascending and descending order.
Now I'm having an error that x and y is protected but how do I use getters correctly in sort, please?
I'm still pretty confused. I tried to use this but doesn't work.
bool Line2D::sortLine2DPtAsc (const Point2D& left, const Point2D& right)
{
return (left.pt1.getX() < right.pt1.getX()) || ((left.pt1.getX() == right.pt1.getX()) && (left.pt1.getY() < right.pt1.getY()));
}
Error using getters:
Line2D.cpp:25:15: error: ‘const class Point2D’ has no member named ‘pt1’
Line2D.cpp:25:34: error: ‘const class Point2D’ has no member named ‘pt1’
Line2D.cpp:25:56: error: ‘const class Point2D’ has no member named ‘pt1’
Line2D.cpp:25:76: error: ‘const class Point2D’ has no member named ‘pt1’
Line2D.cpp:25:97: error: ‘const class Point2D’ has no member named ‘pt1’
Line2D.cpp:25:116: error: ‘const class Point2D’ has no member named ‘pt1’
here's my code:
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
public:
//Constructor
Line2D ();
Line2D (Point2D pt1, Point2D pt2);
//Accessors
Point2D getPt1();
Point2D getPt2();
double getScalarValue(); //returns the value of attribute length
//Mutators
void setPt1 (Point2D pt1);
void setPt2 (Point2D pt2);
static bool sortLine2DPtAsc (const Point2D& left, const Point2D& right) ;
}
bool Line2D::sortLine2DPtAsc (const Point2D& left, const Point2D& right)
{
return (left.x < right.x) || ((left.x == right.x) && (left.y < right.y));
}

Use the version with the getters, but replace left.pt1.getX() with left.getX(), etc.
And I'm guessing that in your Point2D class, you need to change
double getX();
to
double getX() const;
And likewise for getY.
Being passed const Point2D& left, const Point2D& right, you are under contract not to modify any values in those objects. You can only call their members functions that are declared const (they will not change the object).

bool Line2D::sortLine2DPtAsc (const Point2D& left, const Point2D& right)
your parameters are of type Point2D, maybe you meant to have them of type Line2D
if the function is sorting lines, why are you passing it points ;)
pt1 and pt2 are members of the Line2D class not the your Point2D class

Related

passing ‘const hlp2::Point’ as ‘this’ argument discards qualifiers [-fpermissive]

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.

Cannot access public method from another class

I have the following classes:
class Point
{
private:
Vector2d _coordinates;
int _label;
public:
Point(const double x = 0.0, const double y = 0.0);
Point(Vector2d coordinates);
void setCoordinates(const double x, const double y) {_coordinates(x, y);}
const Vector2d& getCoordinates() const {return _coordinates;}
int getPointLabel() const{ return _label;}
void setPointLabel(int label);
class PolygonCutter
{
//output parameters
vector<Point> _newPoints;
vector< vector<Point>> _cuttedPolygons;
public:
void AddLabels(const vector<Point> &points, const vector<const unsigned int>&polygonVertices);
};
I working on PolygonCutter. i need it to be just a manager that uses a methodAddLabels to set the member _label of each Point in vector points to the integers passed as the vector polygonVertices
How can I do it? I am having trouble because I am not able to access the method setPointLabel(int label) from here:
My try contains the following errors:
void AddLabels(const vector<Point> &points, const vector<const unsigned int>&polygonVertices);
{
int originalNumberOfVertices = points.size();
for(int i = 0; i<originalNumberOfVertices ; i++)
{ int label = polygonVertices[i]; // ERROR type 'const vector<const unsigned int>' does not provide a subscript operator
const Point& point = points[i];
point.setPointLabel(label); // ERROR 'this' argument to member function 'setPointLabel' has type 'const Point', but function is not marked const
}
}
in AddLabels method, vector points is const. setPointLabel is non-const member function. non-const member functions can only be invoked for non-const objects. Either
remove const from const vector &points , or
mark function setPointLabel const.

Operator overloading functions

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...

Compare between objects raise compilation error according to order

I've created a Point class and overload his operator==, and a PointSet class that has an array of pointers to Point objects.
Now, when i want to compare between two object I'm running in to unexplained compilation error when I'm tring to compare A==B but not when B==A.
i.e when im fliping the order of the object the function work as ecxpected.
I didnt find any logic between this two cases.
This is Point and PointSet classes (diffrenet header files)
class Point{
public:
Point(int x, int y);
~Point();
bool operator==(const Point& point);
private:
int _x, _y;
};
class PointSet
{
public :
PointSet(const size_t startLen);
~PointSet();
int contains(const Point *point) const
private:
Point **_pointArray;
size_t _size;
};
this is the implementation of the operator==
bool Point::operator ==(const Point& point){
return (this->_x == point.get_x() && this->_y == point.get_y());
}
And this is the function where the problem occur
int PointSet::contains(const Point *point) const{
for (int i=0; i<_size;i++){
if (*point == *_pointArray[i]){ <-----HERE
return i;
}
}
return -1;
}
this ISN'T an error if(*_pointArray[i] == *point)
the compilation error is "Can't compare structures"
Your operator== should be a const function
Try to implement
bool operator==(const Point& lhs, const PointSet& rhs){ /* do actual comparison */ }
as a friend member function. Also include:
bool operator==(const PointSet& lhs, const Point& rhs){ /* do actual comparison */ }.
This is the way you guarantee it will work.
More about this can be found here.

Static member function cannot access protected member of class

My understanding is static member functions can access private, protected members of the class.
Here in my code, sortPoint2DXAsc should be able to access X and Y since it's member function of Point2D. But I get this error:
Point2D.h: In function ‘bool sortPoint2DXAsc(const Point2D&, const Point2D&)’:
Point2D.h:22:7: error: ‘int Point2D::x’ is protected
Point2D.cpp:41:21: error: within this context
Point2D.h:22:7: error: ‘int Point2D::x’ is protected
Point2D.cpp:41:31: error: within this context
Point2D.h:22:7: error: ‘int Point2D::x’ is protected
Point2D.cpp:41:44: error: within this context
Point2D.h:22:7: error: ‘int Point2D::x’ is protected
Point2D.cpp:41:55: error: within this context
Point2D.h:23:7: error: ‘int Point2D::y’ is protected
Point2D.cpp:41:67: error: within this context
Point2D.h:23:7: error: ‘int Point2D::y’ is protected
Point2D.cpp:41:77: error: within this context
Here is my code:
class Point2D
{
protected:
int x;
int y;
public:
//Constructor
Point2D();
Point2D (int x, int y);
//Accessors
int getX();
int getY();
//Mutators
void setX (int x);
void setY (int y);
static bool sortPoint2DXAsc (const Point2D& left, const Point2D& right);
};
bool sortPoint2DXAsc (const Point2D& left, const Point2D& right)
{
return (left.x < right.x) || ((left.x == right.x) && (left.y < right.y));
}
I think you want something like this code tested :
class Point2D {
protected:
int x;
int y;
public:
//Constructor
Point2D();
Point2D (int x, int y);
//Accessors
int getX() const {return x; }
int getY() const {return y;}
//Mutators
void setX (int x) {/*Do something with x*/}
void setY (int y) {/*Do something with y*/}
static bool sortPoint2DXAsc(const Point2D& left, const Point2D& right);
};
bool Point2D::sortPoint2DXAsc (const Point2D& left, const Point2D& right)
{
return (left.getX() < right.getX()) || ((left.getX() == right.getX()) && (left.getY() < right.getY()));
}
You can use static since you're not using this in your function. For example, if you was using this->x instead of left.getX(), you would have received an error because your function is static.
Now, here's a second example where you can access x and y without accessors.
Since you're inside your class definition, x and y are accessible from left and right which are instance of Point2D even if they are protected.