C++ virtual assignment operator - c++

I am trying to call the assignment operator from the base class (shape) in the derived class (point). I get an unresolved external linker error and I don't understand why. I put "virtual" in front of the base assignment operator and placed the base assignment operator inside of my derived class at the end. Where did I go wrong?
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
namespace Joe
{
namespace CAD
{
class Shape
{
private:
int m_id;
public:
Shape(); //default constructor
~Shape(); //destructor
Shape(const Shape& s); //copy constructor
virtual Shape& operator = (const Shape& source); //assignment operator
string ToString() const; //returns id as a string
friend ostream& operator << (ostream& os, const Shape& sh); //global function that can access private members
int ID() const;
};
inline int Shape::ID() const //retrieve ID of shape
{
return m_id;
}
}
}
#endif
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include "shape.h"
namespace Joe
{
namespace CAD
{
class Point: public Shape
{
private:
double m_x;
double m_y;
public:
Point(); //default constructor
~Point(); //destructor
Point(const Point& pt); //copy constructor
Point(double newX, double newY) //constructor accepting x and y coordinates
{
m_x = newX;
m_y = newY;
std::cout << "Point(" << m_x <<","<< m_y <<")" << std::endl;
}
Point(double val); //constructor accepting one double
void X(double newXval) {m_x = newXval;} //default inline setter
void Y(double newYval) {m_y = newYval;} //default inline setter
double X() const; //getter pre-inline
double Y() const; //getter pre-inline
std::string ToString() const; //returns a string description
//distance functions
double Distance() const; //calculate the distance to the origin (0,0)
double Distance(const Point& p) const; //calculate the distance between two points
//operator overloading
Point operator - () const; //negate the coordinates
Point operator * (double factor) const; //scale the coordinates
Point operator + (const Point& p) const; //add coordinates
bool operator == (const Point& p) const; //equally compare operator
Point& operator = (const Point& source); //assignment operator
Point& operator *= (double factor); //scale the coordinates and assign
friend std::ostream& operator << (std::ostream& os, const Point& p); //send to ostream (friend)
Shape& operator = (const Shape& source); // call assignment operator of base class
};
inline double Point::X() const //normal inline getter
{
return m_x;
}
inline double Point::Y() const //normal inline getter
{
return m_y;
}
}
}
#endif
Shape& Shape::operator = (const Shape& source) //assignment operator
{
if (this == &source) //avoid self-assignment
return *this;
cout << "shape assignment" << endl;
m_id = source.m_id;
return *this;
}
Point& Point::operator = (const Point& source) //assign
{
if (this == &source) //avoid self-assignment
return *this;
m_x = source.m_x;
m_y = source.m_y;
return *this;
}

Your problem is that when you call operator = and pass it a Shape, you are calling to Point::operator=(const Shape&), which you do not define, you only define Shape::operator=(const Shape&) and Point::operator=(const Point&). You need to add Point::operator=(const Shape&), for example like this:
Shape& Point::operator = (const Shape& shape) //assign
{
const Point& source = static_cast<Point&>(shape); // or use dynamic_cast here, if you want to be safe
if (this == &source) //avoid self-assignment
return *this;
m_x = source.m_x;
m_y = source.m_y;
return *this;
}

Your problem might be that you define the class in a namespace, but you define the operators outside that namespace without specifying it. Because of this, the compiler can't connect the definition to the declaration.
Ishamael pointed out that you don't specify what happens when a non-Point Shape is assigned to a Point. This is necessary for a virtual assignment operator; see his answer. But this virtual assignment operator could end up doing a lot of unexpected things, like cutting off parts of objects if the wrong types are assigned to each other.
You don't need the virtualness to ensure that the Shape operator also gets called when Point is assigned. Just call the Shape operator inside the Point operator. Making the operator virtual would actually have the opposite effect; the Point operator would override the Shape operator even if the instance you're calling it with is referred to as a Shape.
Point& Point::operator = (const Point& source) //assign
{
if (this == &source) //avoid self-assignment
return *this;
Shape::operator=(source); // also call the base class operator
m_x = source.m_x;
m_y = source.m_y;
return *this;
}

Related

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

Inherit methods implemented in base class with CRTP in C++

I am not very familiar with templates in C++ so I am struggling to solve the following problem.
I have the classes Vector2D and Vector3D but I want them to inherit from a class Vector. To be able to define common functions in Vector I am using CRTP (Curiously Recurring Template Pattern).
This is a shortend version of my Vector class:
#pragma once
#include "../stdafx.h"
template <class VectorImpl>
class Vector {
public:
VectorImpl& operator=(const VectorImpl& other);
virtual const float operator*(const VectorImpl& other) const = 0;
friend const VectorImpl operator*(const float& scalar, const VectorImpl& other);
virtual VectorImpl& operator*=(const float& scalar) = 0;
float abs() const;
virtual std::string toString() const = 0;
virtual ~Vector() = 0 {};
protected:
virtual void copyFrom(const VectorImpl& other) = 0;
};
-----------------------------------------------------------------------------------------
#include "Vector.h"
#include "Vector2D.h" // edit
#include "Vector3D.h" // edit
template <class VectorImpl>
VectorImpl& Vector<VectorImpl>::operator=(const VectorImpl& other) {
this->copyFrom(other);
// edit
// return *this;
return static_cast<VectorImpl&>(*this);
}
template <class VectorImpl>
const VectorImpl operator*(const float& scalar, const VectorImpl& other) {
VectorImpl result = other;
result *= scalar;
return result;
}
template <class VectorImpl>
float Vector<VectorImpl>::abs() const {
const VectorImpl thisImpl = static_cast<const VectorImpl&>(*this); // edit
return std::sqrt(thisImpl * thisImpl);
}
// edit
template class Vector<Vector2D>;
template class Vector<Vector3D>;
And this is a shortend version of my Vector2D class:
#pragma once
#include "../stdafx.h"
#include "Vector.h"
class Vector2D :
public Vector<Vector2D> {
public:
Vector2D();
Vector2D(const Vector2D& other);
Vector2D(const float& xPos, const float& yPos);
const float operator*(const Vector2D& other) const;
Vector2D& operator*=(const float& scalar);
std::string toString() const;
~Vector2D();
protected:
void copyFrom(const Vector2D& other);
private:
float xPos;
float yPos;
};
-----------------------------------------------------------------------------------------
#include "Vector2D.h"
Vector2D::Vector2D() :
xPos(0.0f),
yPos(0.0f) {
}
Vector2D::Vector2D(const Vector2D& other) {
this->copyFrom(other);
}
void Vector2D::copyFrom(const Vector2D& other) {
if (this != &other) {
this->xPos = other.getXPos();
this->yPos = other.getYPos();
}
}
Vector2D::Vector2D(const float& xPos, const float& yPos) {
this->xPos = xPos;
this->yPos = yPos;
}
const float Vector2D::operator*(const Vector2D& other) const {
return this->xPos * other.getXPos() + this->yPos * other.getYPos();
}
Vector2D& Vector2D::operator*=(const float& scalar) {
this->xPos *= scalar;
this->yPos *= scalar;
return *this;
}
std::string Vector2D::toString() const {
std::string ret = std::to_string(this->xPos);
ret += " ";
ret += std::to_string(this->yPos);
return ret;
}
Vector2D::~Vector2D() {
// intentionally left blank
}
The problem is, for every call of a method from Vector2D (Vector3D respectively) that is implemented in Vector I get an LNK2019 error (unresolved external symbol 'symbol' referenced in function 'function').
I am quite sure that I am doing somethin wrong with my template implementation but I can't figure out what it is. I am researching for hours now.
I appreciate any help, so thanks in advance.
EDIT:
I added the necessary instantiations of my template classes but now I get casting errors. Adding
VectorImpl result = static_cast<const VectorImpl>(*this);
or
VectorImpl result = static_cast<VectorImpl>(*this);
doesn't change the problem. What I am I still missing here?
EDIT 2:
Okay of course it hast to be static_cast<const VectorImpl&>(*this).
I also added these casts but now I get that damn linking error again. This time only the functions friend operator* and abs can't be linked. I don't see much difference to the other functions. So why are they problematic?

overload greater than operator with or without friend

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;

Overloading ostream << on one class and output data of another class

class Point2D
{
protected:
int x;
int y;
public:
Point2D () {x=0; y=0;}
Point2D (int a, int b) {x=a; y=b;}
void setX (int);
void setY (int);
int getX ();
int getY ();
};
class Line2D
{
friend ostream& operator<< (ostream&, const Line2D&);
private:
Point2D pt1;
Point2D pt2;
double length;
public:
Line2D () {pt1 = Point2D(); pt2 = Point2D();}
Line2D (Point2D ptA, Point2D ptB) {pt1=ptA; pt2=ptB;}
void setPt1 (Point2D);
void setPt2 (Point2D);
Point2D getPt1 ();
Point2D getPt2 ();
};
ostream& operator<< (ostream &out, const Line2D &l)
{
//out << l.length << endl; //Reading length works perfectly
out << l.getPt1().getX() << endl; //But how to read x from pt1 ?
return out;
}
When I run these codes, I get error saying:
no matching function for call to Line2D::getPt1() const and
note: candidates are: Point2D Line2D::getPt1() <near match>.
If I am only trying to display length by overloading << operator , it works perfectly. But when I try to printx and y of Class::Point2D, I get error.
So what should be the proper way to print out x and y ?
Your operator (rightly) takes a const reference. So any methods called via that reference must be const. For example,
Point2D getPt1 () const;
^^^^^
You should also make the Point2D class getters const.

"Error: no matching function for call distance::distance()" ...Can´t declare an object from a class in main?

Im new at OOP and I keep having this error in this task. Maybe you can help me out.
This right here is the Class header file:
class Distance : public Magnitude
{
private:
double Cantidad;
char* Unidad;
public:
Distance(double c, char* u);
Distance(const Distance& d);
double getDistance(){return Cantidad;}
void setDistance(double val) {Cantidad=val;}
char* getUnidad(){return Unidad;}
void setUnidad(char* uni) {Unidad=uni;}
virtual ~Distance();
Distance& operator =(const Distance & d);
Distance operator +(const Distance & d);
Distance operator -(const Distance & d);
Distance operator *(const Distance & d);
Distance operator /(const Distance & d);
friend ostream& operator << (ostream &o,const Distance &d);
friend istream& operator >> (istream &o, Distance &d);
};
This over here is the cpp file, where I made the definitions:
#include "Distance.h"
Distance::Distance(double c, char* u)
{
Cantidad=c;
Unidad=u;
}
Distance::Distance(const Distance& d)
{
cout << "[***] NumComplejo -> Constructor por copia " << endl;
Cantidad = d.Cantidad;
Unidad = d.Unidad;
}
Distance::~Distance()
{
//dtor
}
And finally, this is where the error appears, in main, where I try to declare an object from the class Distance.
int main(int argc, char *argv[])
{
Distance d1; **/*right here*/**
EDIT:
If I typed: Distance d1=Distance(1231,"CSDVS"); it does work, but I need an empty objet so I can use the overload on >> operator
The error is caused by the fact that you are trying to initialize an object with a default constructor that you haven't defined.
[...] but I need an empty objet so I can use the overload on >> operator.
You can do this by simply defining a default constructor:
// …
Distance::Distance()
: Cantidad(0)
, Unidad(nullptr)
{}
// …
You, also, probably want to use std::string for strings.