I have included the minimal amount of code to replicate this issue.
I would like it if I could assign a Furlong object to a Metric object and vice versa but instead the compiler gives me this error:
no operator "=" matches these operands
Header file:
#ifndef DISTANCE_H_
#define DISTANCE_H_
using namespace std;
#include <iostream>
//const to facilitate conversions between the two classes
const double FEET_IN_METERS = 0.3048;
const double FURLONG_TO_FEET = 660;
class Furlong;
class Metric;
class Furlong {
public:
Furlong(int fur = 0, int yar = 0, int fee = 0.0);
// Copy Constructor.
Furlong( const Furlong& rhs );
// Destructor.
~Furlong(void);
// Assignment operator=.
Furlong& operator=( Furlong& rhs);
int furlong;
int yards;
double feet;
};
class Metric {
public:
Metric(int kilo = 0, double mete = 0.0);
Metric (const Metric& rhs);
~Metric(void);
Metric& operator=(Metric& rhs);
Metric (Furlong& f);
operator Furlong();
int kilometers;
double meters;
};
#endif
Class Definition file:
#include <stdlib.h>
#include "Distances.h"
//FURLONG CLASS DEFINITIONS
//Constructor
Furlong::Furlong(int fur, int yar, int fee) {
furlong = fur;
yards = yar;
feet = fee;
}
// Copy Constructor.
Furlong::Furlong( const Furlong& rhs ) {
furlong = rhs.furlong;
yards = rhs.yards;
feet = rhs.feet;
}
// Destructor.
Furlong::~Furlong(void) {
}
// Assignment operator=.
Furlong& Furlong::operator=( Furlong& rhs) {
furlong = rhs.furlong;
yards = rhs.yards;
feet = rhs.feet;
return *this;
}
//METRIC CLASS DEFINITONS
Metric::Metric(int kilo, double mete) {
kilometers = kilo;
meters = mete;
}
Metric::Metric(const Metric& rhs) {
kilometers = rhs.kilometers;
meters = rhs. meters;
}
Metric::~Metric(void) {
}
Metric& Metric::operator=(Metric& rhs) {
kilometers = rhs.kilometers;
meters = rhs.meters;
return *this;
}
// conversion constructor
Metric::Metric (Furlong& f) {
kilometers = 3;
meters = 2.0;
}
//conversion operator
Metric::operator Furlong() {
return Furlong(1, 2, 3.0);
}
Main file:
#include <stdlib.h>
#include "Distances.h"
using namespace std;
int main() {
Furlong one(1,2,3.0);
Furlong three(4,5,6.0);
Metric two(7,8.0);
Metric four(9, 10.0);
one = two;
four = three;
return 0;
}
I would like object two to be converted to type Furlong then assigned to object one. In addition, object three should be converted to type Metric then assigned to object four