Bigint运算符>>过载
Bigint operator >> overload
这是我的运算符的代码>>重载。它应该将数字上升到分号并将它们放入一个 bigint。
std::istream& operator>>(std::istream& is, bigint& bi) {
int i = 0;
char ch;
char temp[SIZE];
// grabs the first character in the file
is >> ch;
temp[i] = ch;
++i;
// while loop grabs the rest of the characters
// up to the semicolon
while(ch != ';') {
is >> ch;
temp[i] = ch;
++i;
}
// temp is stored in the bigint ref
bi = bigint(temp);
return is;
}
我遇到的问题是,当我运行它时,它会给我额外的输出。例如:当我输入"34;"作为输入时,生成的 bigint 将是"3411"。谁能告诉我我做错了什么?
您不会以 null 终止字符串temp
.添加这个:
temp[i - 1] = ' ';
bi = bigint(temp);
请注意,-1
将删除您可能也不需要的分号。如果出于任何原因要保留分号,请将其更改为 temp[i]
。
您还应该向 while 循环添加检查,以确保缓冲区大小不会溢出。
您保证分号在末尾temp
。分号可能会弄乱bigint
对该字符串所做的任何解析。在将分号插入temp
之前更改循环以测试分号:
std::istream& operator>>(std::istream& is, bigint& bi)
{
char temp[SIZE] = {}; // zero out the array so the end is null terminated
char c;
for(int i = 0; i < SIZE-1 && is >> c && c != ';'; ++i)
{
temp[i] = c;
}
bi = bigint(temp);
return is;
}
相关文章:
- ostream过载时的缓冲区冲洗
- gcc和c++17的过载解析失败
- C++Cast运算符过载
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- EASTL矢量<向量<int>>连续的
- 过载'operator new'如何导致无限循环?
- "Inverse SFINAE"避免模棱两可的过载
- 操作员C++的模棱两可的过载
- C++中>>操作员过载时出现问题?
- 基于返回类型的转换和过载扣除
- 没有可行的过载'='错误,可能导致这种情况的原因是什么?
- 是否存在包含负号的isdigit函数(过载)
- C++许多 SFINAE 风格的过载
- C++函数过载会导致 SEG 故障
- SFINAE不能防止模棱两可的操作员过载吗?
- 如何解决不明确的运算符过载问题?
- 屏幕插入运算符<<的运算符过载问题
- ostream 运算符<< 为获取 STL 容器而过载,传递 std::string 会破坏它?
- 在这种情况下是私有的吗?试图使操作员<<过载
- C 操作员&gt;&gt;与突变器过载