断言“in”失败 - c++
以下是分数类的客户端程序的一部分。我编写了该类,现在正在使用给定的客户端程序对其进行测试。当我尝试运行它时,出现以下错误:
断言
'in'
失败。
代码:
bool eof(ifstream& in);
cout << "\n----- Now reading Fractions from file\n";
ifstream in("fraction.data");
assert(in);
while (!eof(in)) {
Fraction f;
if (in.peek() == '#') {
in.ignore(128, '\n'); //skip this line, it's a comment
} else {
in >> f;
cout << "Read fraction = " << f << endl;
}
作为 C++ 的相对初学者,我真的不明白这部分代码应该做什么:
ifstream in("fraction.data");
assert(in);
当我尝试调试并达到这一点时,它说:
没有可用的源代码
“__kernel_vsyscall() at 0x12e416”
所以是的,总之,我对为什么会发生这种情况一无所知:P
编辑:以下是包含语句
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
The following is part of client program for a fraction class. I wrote the class and now am testing it with the given client program. When I try to run it, I get this error:
Assertion
'in'
failed.
Code:
bool eof(ifstream& in);
cout << "\n----- Now reading Fractions from file\n";
ifstream in("fraction.data");
assert(in);
while (!eof(in)) {
Fraction f;
if (in.peek() == '#') {
in.ignore(128, '\n'); //skip this line, it's a comment
} else {
in >> f;
cout << "Read fraction = " << f << endl;
}
As a relative beginner to C++, I don't really understand what this part of the code is supposed to be doing:
ifstream in("fraction.data");
assert(in);
And when I try to debug and I get to that point, it says:
No source available for
"__kernel_vsyscall() at 0x12e416"
So yeah, in conclusion I'm pretty clueless about why this happening :P
EDIT: Here are the include statements
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果表达式的计算结果为 false,则assert() 失败。
失败,因为 in (输入文件)的计算结果为 false。您的代码无法打开名为“fraction.data”的文件。如果 in 是有效的输入文件流,则断言(in)将通过,您将继续处理您的业务。
简短回答-> “找不到文件”或“无法在此处创建文件”。
The assert() fails if the expression evaluates to false.
fails because in (the input file) evaluates to false. Your code is unable to open a file called "fraction.data". If in were a valid input file stream, assert(in) would pass, and you'd go on about your business.
Short answer -> "File not found" or "Can't create a file here".
assert
是一个运行时检查,用于验证其参数是否正确。在这种情况下,您的文件无效。assert
is a runtime check that verifies its argument is true. In this case, your file is not valid.所以我终于明白了。我只需要将“fraction.data”文件移动到主项目目录。之前,我将它放在目录中的源文件夹中。
So I finally got it. I just need to move my "fraction.data" file to main project directory. Before, I had it in the source folder within the directory.