ifstream::eof在if语句中时抛出类型错误
ifstream::eof throws a type error when in if statement
我有一个类a,它有一个std::ifstream filestr成员。在其中一个类函数中,我测试流是否已达到eof。
class A
{
private:
std::ifstream filestr;
public:
int CalcA(unsigned int *top);
}
然后在cpp文件中,我有
int CalcA(unsigned int *top)
{
int error;
while(true)
{
(this->filestr).read(buffer, bufLength);
if((this->filestr).eof);
{
error = 1;
break;
}
}
return error;
}
我得到一个编译错误
error: argument of type ‘bool (std::basic_ios<char>::)()const’ does not match ‘bool’
有人能告诉我如何正确使用eof吗?或者我出现这个错误的其他原因?
eof
是一个函数,因此需要像其他函数一样调用:eof()
。
也就是说,给定的读取循环可以在不调用eof()
的情况下更正确地写入(考虑文件结尾以外的其他故障可能性),但将读取操作转换为循环条件:
while(filestr.read(buffer, bufLength)) {
// I hope there's more to this :)
};
尝试
if(this->filestr).eof())
(this->filestr).eof
单独是指向成员方法的指针。if
语句需要bool
类型的表达式。因此,您需要调用该方法。这将成功,因为它返回一个bool
值。
(this->filestr).eof
没有调用该函数。(this->filestr).eof()
是.:-)这解释了您的错误。
相关文章:
- 我的模板类方法返回错误类型?
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- glDrawElements GL 错误类型 = 0x824c,严重性 = 0x9146,消息 = GL_INVALID
- Eclipse/CDT_C++给出"语义错误_"类型XXX无法解决"。项目运行
- turbo 为什么我的C++代码中出现错误类型名称预期"错误?
- "auto"推断出 hashtable_policy.h 中的错误类型
- 设计自己的结果/两者之一:是否应该始终指定成功类型和错误类型
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 在 vscode 上编译错误.类型说明符是 C++11 扩展
- 不允许使用错误类型
- 当读取错误类型时,CIN覆盖我的初始化值
- C++:"错误:类型'const char*'和'const char [28]'的操作数无效到二进制'ope
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 在shared_from_this()中的错误类型(在继承类中)(是否有dyn.type-ware shared Poi
- 为什么缩小转换范围不能防止错误类型的map.insert()失败
- 程序总是返回二进制'>>':找不到运算符,它采用错误类型的左操作数
- 错误:类型为 '.&' 来自类型的临时 '..*康斯特'
- 涉及将要馈送的向量转换为execvp的错误类型
- C++中是否存在错误类型