オーバーロードされた代入演算子を使用するとエラーが発生するのに、コンパイラが提供する演算子を使用できないのはなぜですか?

H-005

私は最も重要な部分だけを置くように最善を尽くしました:

header.h

#include <cstdint>
#include <string>
#include <vector>
#include "byte.h" /// doesn't matter what's in here
#pragma once

using int64 = int64_t;
using int32 = int32_t;

/// FORWARD-DECLARATIONS
class BigInt;

/// *** CLASS BIGINT ***

class BigInt
{
    std::vector<byte> vec;
    bool neg; /// true if negative

public:
    /// CONSTRUCTORS
    BigInt ();
    BigInt (const int64);

    /// OPERATORS
    /// ASSIGNMENT
    void operator = (const BigInt&);

    /// ARITHMETIC
    BigInt operator + (const BigInt&);
    BigInt operator - (const BigInt&);
};

/// DEFINITIONS
/// CONSTRUCTORS
BigInt::BigInt () : vec(1), neg(0) {}
BigInt::BigInt (const int64 x) : vec(x), neg(0) {}

/// OPERATORS
/// ASSIGNMENT
void BigInt::operator = (const BigInt &p)
{
    (*this).vec = p.vec;
    (*this).neg = p.neg;
}

/// ARITHMETIC
BigInt BigInt::operator + (const BigInt &p)
{
    BigInt a = *this;
    BigInt b = p;
    BigInt res;

    if (a.neg ^ b.neg)
    {
        if (a.neg)
            std::swap(a, b);
        b.neg = 0;
        /*return*/ res = a.BigInt::operator - (b); /// I get an error if I don't comment this out
        return res;
    }

    return res;
}

BigInt BigInt::operator - (const BigInt &p)
{
    BigInt a = *this;
    BigInt b = p;
    BigInt res;

    return res;
}

BigInt BigInt::operator + (const BigInt &p)、私はエラーを取得する私は返すようにしようとするreturn res = a.BigInt::operator - (b);が、私はこのようにそれを返さないとき:res = a.BigInt::operator - (b); return res;ただし、これは=演算子をオーバーロードした場合にのみ発生し、コンパイラが提供する演算子では発生しません。

エラー: タイプ「void」の戻り値から関数の戻り値のタイプ「BigInt」への実行可能な変換はありません return res = a.BigInt::operator - (b);

ソンユアンヤオ

エラーメッセージが言ったように、でoperator=返すvoidことができないあなたのリターンは、を返すことになっています。return res = a.BigInt::operator - (b);operator +BigInt

(コンパイラーによって生成されたものとoperator=同様に)戻り値BigInt&として宣言する必要があります。

BigInt& BigInt::operator = (const BigInt &p)
{
    (*this).vec = p.vec;
    (*this).neg = p.neg;
    return *this;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ