使用 <iostream> header 解决了意外错误

Unexpected error resloved by using <iostream> header

抱歉,如果此代码太长。当我试图重现这个问题时,它显示了预期的结果。

所以我试图制作一个 BigInteger header:

BIGINT.h

#ifndef BIGINT_H
#define BIGINT_H

//#include <iostream> 

#include <string>
#include <vector>
#include <cctype>
#include <cassert>
#include <cstddef>

extern short DEFAULT_BASE;

class BigInt
{
private:
    bool m_sign; // (0/false) mean negative, (1/true) mean positive
    std::string m_number;
    unsigned char m_base;

public:
    // constructor
    BigInt(std::string number = "0", unsigned char base = DEFAULT_BASE);
    BigInt(unsigned char base, std::string number = "0");
    BigInt(const BigInt &copy);

    //destructor
    ~BigInt();

    // get member value
    std::string number() const;
    unsigned char base() const;
    bool is_number_positive() const;

    // I/O overload
    friend std::ostream& operator<< (std::ostream& out, const BigInt& bigInt);
    friend std::istream& operator>> (std::istream& in, BigInt& bigInt);


};


#endif // BIGINT_H

BIGINT.cpp

#include "BIGINT.h"

short DEFAULT_BASE = 10;

// constructor
BigInt::BigInt(std::string number, unsigned char base)
{
{
    {
        int i{0};
        int numberOfSubtraction{0};
        while (number[i] == '-' || number[i] == '+')
        {
            numberOfSubtraction += number[i] == '-';
            ++i;
        }

        m_sign = numberOfSubtraction % 2 == 0;

        number = number.substr(i);
    }
    // std::cerr << base << ' ';
    assert(base >= 2 && base <= 36);
    for(char &i : number)
    {
        if(isdigit(i)) assert(i - '0' < base);
        else if(isalpha(i)) assert(i - 'A' + 10 < base);
        m_number += toupper(i);
    }
    m_base = base;


}

BigInt::BigInt(unsigned char base, std::string number)
{
    {
        int i{0};
        int numberOfSubtraction{0};
        while (number[i] == '-' || number[i] == '+')
        {
            numberOfSubtraction += number[i] == '-';
            ++i;
        }

        m_sign = numberOfSubtraction % 2 == 0;

        number = number.substr(i);
    }
    // std::cerr << base << ' ';
    assert(base >= 2 && base <= 36);
    for(char &i : number)
    {
        if(isdigit(i)) assert(i - '0' < base);
        else if(isalpha(i)) assert(i - 'A' + 10 < base);
        m_number += toupper(i);
    }
    m_base = base;

}

BigInt::BigInt(const BigInt &copy)
{
    m_number = copy.m_number;
    m_base = copy.m_base;
}

// destructor
BigInt::~BigInt() = default;

// get member value
std::string BigInt::number() const {return m_number;}
unsigned char BigInt::base() const {return m_base;}
bool BigInt::is_number_positive() const {return m_sign;}

// I/O overload
std::ostream& operator<< (std::ostream& out, const BigInt& bigInt)
{
    if(!bigInt.m_sign) out << '-';
    out << bigInt.m_number;

    return out;
}

std::istream& operator>> (std::istream& in, BigInt& bigInt)
{
    std::string number;
    in >> number;
    {
        int i{0};
        int numberOfSubtraction{0};
        while (number[i] == '-' || number[i] == '+')
        {
            numberOfSubtraction += number[i] == '-';
            ++i;
        }

        bigInt.m_sign = numberOfSubtraction % 2 == 0;

        number = number.substr(i);
    }
    for(char &i : number)
    {
        if(isdigit(i)) assert(i - '0' < DEFAULT_BASE);
        else if(isalpha(i)) assert(i - 'A' + 10 < DEFAULT_BASE);
        bigInt.m_number += toupper(i);
    }
    return in;
}

main.cpp

#include "BIGINT.h"

#include <iostream>
#include <string>

int main()
{
    BigInt bigInt{"+-+---+-+--+1", 10};
    std::cout << bigInt ;
}

所以我 运行 这个,它断言: Assertion failed: base >= 2 && base <= 36, file D:\phamngocphuc\C++ project\BigInt\BIGINT.cpp, line 49,这是意外的,因为:

首先,它应该调用这个构造函数BigInt(std::string number = "0", unsigned char base = DEFAULT_BASE);

而不是这个BigInt(unsigned char base, std::string number = "0");

而我的基础实际上应该是好的:(10 >= 2 && 10 <= 36)true

其次,当我在BIGINT.h中#include <iostream>进行调试时,运行它实际上显示-1这是预期的结果。

那么为什么会这样?

当您将有问题的构造函数标记为已删除时 Demo

编译器发现它被使用的地方,(它不是你想的地方):

存在问题:

std::ostream& operator<< (std::ostream& out, const BigInt& bigInt)
{
    if(!bigInt.m_sign) out << '-';
    out << bigInt.m_number;

    return out;
}

没有 include <iostream>(看起来你有一些来自其他 std include 的前向声明可以使用这些类型),out << '-'; 只是作为候选 std::ostream& operator<< (std::ostream& out, const BigInt& bigInt) (递归不过调用也会有问题。

所以从 '-' (BigInt(unsigned char base, std::string number = "0");) 构造一个 BigInt

然后是你的错误。