【发布时间】:2021-12-03 11:38:21
【问题描述】:
抱歉,如果此代码太长。当我试图重现这个问题时,它显示了预期的结果。
所以我试图制作一个 BigInteger 标题:
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 ©);
//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 ©)
{
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,这是预期的结果。
那么为什么会这样呢?
【问题讨论】:
-
你是如何编译该代码的?因为它甚至不应该在没有
iostream标头的情况下编译。 -
@Konrad Rudolph 我在 main 中做了
include <iostream>,但在 BIGINT.h 中没有。请告诉我为什么不能编译 -
@justANewbie 但它需要包含在
BiGINT.cpp中!请再次说明您是如何编译代码的。 -
@KonradRudolph:与 Code::Blocks 无关,只是标准包含做了一些其他包含/转发声明,不幸的是这些声明也被导出了:/ 模块应该可以解决这个问题。
-
@Jarod42 啊,对,我忽略了头文件中的其他包含。好的,所以 C::B 可能不是这里的罪魁祸首。
标签: c++ class c++11 constructor