英文:
How to resolve "error: no match for ‘operator=’" when overloading arithmetic operators in a template
问题
The error you're encountering is related to the assignment (z = x + y
) in your main.cpp
file. It seems there's no defined assignment operator (operator=
) for your fractionType
class. To resolve this issue, you should add an assignment operator to your fractionType
class definition in fractionType.h
. Here's the updated code for the assignment operator:
template <class T>
fractionType<T>& operator=(const fractionType<T>& other)
{
if (this != &other) // Check for self-assignment
{
numerator = other.numerator;
denominator = other.denominator;
}
return *this;
}
Make sure to add this code within your fractionType
class definition in fractionType.h
. This should resolve the error you're encountering.
英文:
I'm trying to make a fractionType class with functional arithmetic with objects of the same class, I'm also using a template so that the fractionType object can be constructed with an int, float, or double. I've been looking all over for solutions to one problem after another, so I'm needing help resolving the above error in this code:
main.cpp:
#include <iostream>
#include "fractionType.h"
using namespace std;
int main()
{
fractionType<int> x(4, 2);
fractionType<double> y(5.2, 6.8);
fractionType<float> z(1.0, 1.0);
z = x + y;
return 0;
}
fractionType.h:
#ifndef FRACTIONTYPE_H
#define FRACTIONTYPE_H
using namespace std;
template <class T>
class fractionType;
template <class T>
class fractionType
{
public:
explicit fractionType();
explicit fractionType<T>(T num, T den);
T numerator;
T denominator;
};
template <class T>
fractionType<T>::fractionType()
{
}
template <class T>
fractionType<T>::fractionType(T num, T den)
{
numerator = num;
denominator = den;
}
template <typename U, typename V>
fractionType<int> operator + (const fractionType<U>& fraction1, const fractionType<V>& fraction2)
{
fractionType<int> tempFraction(1,1);
tempFraction.numerator = (fraction1.numerator * fraction2.denominator + fraction1.denominator * fraction2.numerator);
tempFraction.denominator = (fraction1.denominator * fraction2.denominator);
return tempFraction;
}
#endif
The error:
main.cpp: In function ‘int main()’:
main.cpp:10:13: error: no match for ‘operator=’ (operand types are ‘fractionType’ and ‘fractionType’)
10 | z = x + y;
| ^
In file included from main.cpp:2:
fractionType.h:10:7: note: candidate: ‘fractionType& fractionType::operator=(const fractionType&)’
10 | class fractionType
| ^~~~~~~~~~~~
fractionType.h:10:7: note: no known conversion for argument 1 from ‘fractionType’ to ‘const fractionType&’
fractionType.h:10:7: note: candidate: ‘fractionType& fractionType::operator=(fractionType&&)’
fractionType.h:10:7: note: no known conversion for argument 1 from ‘fractionType’ to ‘fractionType&&’
Any advice?
答案1
得分: 2
In the statement z = x + y;
, the expression x + y
returns a fractionType<int>
, but z
is a fractionType<float>
instead. You have not defined a conversion to allow that assignment, hence the error.
你在语句 z = x + y;
中,表达式 x + y
返回的是一个 fractionType<int>
,但 z
却是一个 fractionType<float>
。你没有定义转换来允许这种赋值,因此出现了错误。
You can either:
- define a copy constructor that lets you construct
*this
from other types offractionType
objects, eg:
你可以选择:
- 定义一个复制构造函数,允许你从其他类型的
fractionType
对象构造*this
,例如:
template <class T>
class fractionType
{
public:
...
template <typename U>
fractionType(const fractionType<U> &src) {
numerator = static_cast<T>(src.numerator);
denominator = static_cast<T>(src.denominator);
}
};
-
define an assignment operator that lets you assign other types of
fractionType
objects to*this
, eg: -
定义一个赋值运算符,允许你将其他类型的
fractionType
对象赋给*this
,例如:
template <class T>
class fractionType
{
public:
...
template <typename U>
fractionType<T>& operator=(const fractionType<U> &rhs) {
numerator = static_cast<T>(rhs.numerator);
denominator = static_cast<T>(rhs.denominator);
return *this;
}
};
-
define a conversion operator that lets you convert
*this
to other types offractionType
objects, eg: -
定义一个转换运算符,允许你将
*this
转换为其他类型的fractionType
对象,例如:
template <class T>
class fractionType
{
public:
...
template <typename U>
operator fractionType<U>() const {
return fractionType<U>(static_cast<U>(numerator), static_cast<U>(denominator));
}
};
以上是三种解决方法。
英文:
In the statement z = x + y;
, the expression x + y
returns a fractionType<int>
, but z
is a fractionType<float>
instead. You have not defined a conversion to allow that assignment, hence the error.
You can either:
- define a copy constructor that lets you construct
*this
from other types offractionType
objects, eg:
template <class T>
class fractionType
{
public:
...
template <typename U>
fractionType(const fractionType<U> &src) {
numerator = static_cast<T>(src.numerator);
denominator = static_cast<T>(src.denominator);
}
};
- define an assignment operator that lets you assign other types of
fractionType
objects to*this
, eg:
template <class T>
class fractionType
{
public:
...
template <typename U>
fractionType<T>& operator=(const fractionType<U> &rhs) {
numerator = static_cast<T>(rhs.numerator);
denominator = static_cast<T>(rhs.denominator);
return *this;
}
};
- define a conversion operator that lets you convert
*this
to other types offractionType
objects, eg:
template <class T>
class fractionType
{
public:
...
template <typename U>
operator fractionType<U>() const {
return fractionType<U>(static_cast<U>(numerator), static_cast<U>(denominator));
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论