如何在模板中重载算术运算符时解决“error: no match for ‘operator=’”错误。

huangapple go评论81阅读模式
英文:

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 &lt;iostream&gt;
#include &quot;fractionType.h&quot;
using namespace std;

int main()
{
    fractionType&lt;int&gt; x(4, 2);
    fractionType&lt;double&gt; y(5.2, 6.8);
    fractionType&lt;float&gt; z(1.0, 1.0);
    z = x + y;
    return 0;
}

fractionType.h:

#ifndef FRACTIONTYPE_H
#define FRACTIONTYPE_H
using namespace std;

template &lt;class T&gt;
class fractionType;


template &lt;class T&gt;
class fractionType
{
    public:
    explicit fractionType();
    explicit fractionType&lt;T&gt;(T num, T den);
    T numerator;
    T denominator;
};

template &lt;class T&gt;
fractionType&lt;T&gt;::fractionType()
{
    
}

template &lt;class T&gt;
fractionType&lt;T&gt;::fractionType(T num, T den)
{
    numerator = num;
    denominator = den;
}

template &lt;typename U, typename V&gt;
fractionType&lt;int&gt; operator + (const fractionType&lt;U&gt;&amp; fraction1, const fractionType&lt;V&gt;&amp; fraction2)
{
    fractionType&lt;int&gt; 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&amp; fractionType::operator=(const fractionType&amp;)’
   10 | class fractionType
      |       ^~~~~~~~~~~~
fractionType.h:10:7: note:   no known conversion for argument 1 from ‘fractionType’ to ‘const fractionType&amp;’
fractionType.h:10:7: note: candidate: ‘fractionType&amp; fractionType::operator=(fractionType&amp;&amp;)’
fractionType.h:10:7: note:   no known conversion for argument 1 from ‘fractionType’ to ‘fractionType&amp;&amp;’

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:

  1. define a copy constructor that lets you construct *this from other types of fractionType objects, eg:

你可以选择:

  1. 定义一个复制构造函数,允许你从其他类型的 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);
    }
};
  1. define an assignment operator that lets you assign other types of fractionType objects to *this, eg:

  2. 定义一个赋值运算符,允许你将其他类型的 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;
    }
};
  1. define a conversion operator that lets you convert *this to other types of fractionType objects, eg:

  2. 定义一个转换运算符,允许你将 *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&lt;int&gt;, but z is a fractionType&lt;float&gt; instead. You have not defined a conversion to allow that assignment, hence the error.

You can either:

  1. define a copy constructor that lets you construct *this from other types of fractionType objects, eg:
template &lt;class T&gt;
class fractionType
{
public:
    ...
    template &lt;typename U&gt;
    fractionType(const fractionType&lt;U&gt; &amp;src) {
        numerator = static_cast&lt;T&gt;(src.numerator);
        denominator = static_cast&lt;T&gt;(src.denominator);
    }
};
  1. define an assignment operator that lets you assign other types of fractionType objects to *this, eg:
template &lt;class T&gt;
class fractionType
{
public:
    ...
    template &lt;typename U&gt;
    fractionType&lt;T&gt;&amp; operator=(const fractionType&lt;U&gt; &amp;rhs) {
        numerator = static_cast&lt;T&gt;(rhs.numerator);
        denominator = static_cast&lt;T&gt;(rhs.denominator);
        return *this;
    }
};
  1. define a conversion operator that lets you convert *this to other types of fractionType objects, eg:
template &lt;class T&gt;
class fractionType
{
public:
    ...
    template &lt;typename U&gt;
    operator fractionType&lt;U&gt;() const { 
        return fractionType&lt;U&gt;(static_cast&lt;U&gt;(numerator), static_cast&lt;U&gt;(denominator));
    }
};

huangapple
  • 本文由 发表于 2023年5月17日 08:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定