断言“in”失败 - c++

发布于 2024-12-08 12:27:54 字数 1091 浏览 0 评论 0原文

以下是分数类的客户端程序的一部分。我编写了该类,现在正在使用给定的客户端程序对其进行测试。当我尝试运行它时,出现以下错误:

断言'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-12-15 12:27:54

如果表达式的计算结果为 false,则assert() 失败。

assert(in)

失败,因为 in (输入文件)的计算结果为 false。您的代码无法打开名为“fraction.data”的文件。如果 in 是有效的输入文件流,则断言(in)将通过,您将继续处理您的业务。

简短回答-> “找不到文件”或“无法在此处创建文件”。

The assert() fails if the expression evaluates to false.

assert(in)

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".

爱冒险 2024-12-15 12:27:54

assert 是一个运行时检查,用于验证其参数是否正确。在这种情况下,您的文件无效。

assert is a runtime check that verifies its argument is true. In this case, your file is not valid.

只等公子 2024-12-15 12:27:54

所以我终于明白了。我只需要将“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文