我已经包含了最少量的代码来复制这个问题。
如果我可以将一个Furlong对象分配给一个Metric对象,反之亦然,我想它,但编译器给了我这个错误:
没有运算符“=”匹配这些操作数
标题文件:
#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
类定义文件:
#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);
}
主档案:
#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;
}
我希望将对象2转换为Furlong类型然后分配给对象1。此外,对象3应转换为Metric类型,然后分配给对象4
答案 0 :(得分:0)
您只需要明确转换,编译器就可以找到路径:
one = (Furlong) two;
four = (Metric) three;
答案 1 :(得分:0)
问题,不管你信不信,只是缺乏const
正确性。
class Furlong
{
public:
// ...
// Assignment operator=.
Furlong& operator=( Furlong& rhs);
// ...
};
class Metric
{
public:
// ...
Metric& operator=(Metric& rhs);
// ...
};
当您尝试将Furlong
分配给Metric
时,会发生什么:
Metric::operator=()
需要Furlong
,或者需要[cv] Furlong&
。Metric (Furlong& f)
。Metric
,然后用于调用Metric& Metric::operator=(Metric&)
。Metric::operator=()
的参数不是const
,并且没有采用右值引用,因此它无法绑定到临时对象。由于没有其他可能的转换可用,因此编译器会发出错误。同样,在尝试将Metric
分配给Furlong
时,Metric::operator Furlong()
用于创建临时Furlong
,但Furlong::operator=()
无法接受临时作为论据,使转变成为不可能。
因此,解决方案很简单:既然operator=()
都没有修改rhs
,我们会通过const
引用来接受它。
class Furlong
{
public:
// ...
// Assignment operator=.
Furlong& operator=(const Furlong& rhs);
// ...
};
class Metric
{
public:
// ...
Metric& operator=(const Metric& rhs);
// ...
};
Furlong& Furlong::operator=(const Furlong& rhs)
{
// ...
}
Metric& Metric::operator=(const Metric& rhs)
{
// ...
}
这允许rhs
绑定到临时对象,例如Furlong
生成的Metric::operator Furlong()
和Metric
生成的Metric(Furlong&)
。 The code will then compile successfully