C++构造函数中的 std::ifstream 问题

发布于 2024-09-25 16:59:31 字数 851 浏览 1 评论 0原文

我对这段代码有一个问题:

#include <fstream>

struct A
{   
    A(std::ifstream input)
    {
        //some actions
    }
};

int main()
{
    std::ifstream input("somefile.xxx");

    while (input.good())
    {
        A(input);
    }

    return 0;
}

G++ 向我输出这个:

$ g++ file.cpp
file.cpp: In function `int main()':
file.cpp:17: error: no matching function for call to `A::A()'
file.cpp:4: note: candidates are: A::A(const A&)
file.cpp:6: note:                 A::A(std::ifstream)

将其更改为这个后,它会编译(但这并不能解决问题):

#include <fstream>

struct A
{   
    A(int a)
    {
        //some actions
    }
};

int main()
{
    std::ifstream input("dane.dat");

    while (input.good())
    {
        A(5);
    }

    return 0;
}

有人可以解释一下出了什么问题以及如何修复它吗?谢谢。

I've got a problem with this code:

#include <fstream>

struct A
{   
    A(std::ifstream input)
    {
        //some actions
    }
};

int main()
{
    std::ifstream input("somefile.xxx");

    while (input.good())
    {
        A(input);
    }

    return 0;
}

G++ outputs me this:

$ g++ file.cpp
file.cpp: In function `int main()':
file.cpp:17: error: no matching function for call to `A::A()'
file.cpp:4: note: candidates are: A::A(const A&)
file.cpp:6: note:                 A::A(std::ifstream)

After changing it to this it compile (but that is not solving the problem):

#include <fstream>

struct A
{   
    A(int a)
    {
        //some actions
    }
};

int main()
{
    std::ifstream input("dane.dat");

    while (input.good())
    {
        A(5);
    }

    return 0;
}

Can someone explain me what's wrong and how to fix it? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

呆橘 2024-10-02 16:59:38

流是不可复制的。

所以你需要通过引用传递。

struct A
{   
    A(std::ifstream& input)
                 ^^^^^
    {
        //some actions
    }
};

Streams are non copyable.

So you need to pass by reference.

struct A
{   
    A(std::ifstream& input)
                 ^^^^^
    {
        //some actions
    }
};
梦途 2024-10-02 16:59:37

ifstream 没有复制构造函数。 A(std::ifstream input) 表示“A 的构造函数,采用 ifstream 按值”。这要求编译器制作流的副本以传递给构造函数,但它无法执行此操作,因为不存在此类操作。

您需要通过引用传递流(意思是,“使用相同的流对象,而不是它的副本。”)因此,将构造函数签名更改为 A(std::ifstream& input)。请注意&符号,它的意思是“引用”,对于函数参数来说,意思是“通过引用传递此参数,而不是通过值


风格注释: while 循环的主体 A(input); 构造了一个 A 类型的结构,然后几乎立即当 while 循环循环时被销毁。您确定这是您想要执行的操作吗?如果此代码完整,那么将其设为一个函数或 的成员函数会更有意义。 >在循环外部构建的

static void process(std::istream& stream)
{
    // some actions
    // note stream is declared as std::istream&; this lets you pass
    // streams that are *not* file-based, if you need to
}

int main()
{
    std::ifstream input("somefile.xxx");

    while (input.good())
    {
        process(input);
    }

    return 0;
}

OR

struct A
{   
    A()
    {
        // default constructor for struct A
    }

    void process(std::istream& stream)
    {
        // some actions
    }
};

int main()
{
    std::ifstream input("somefile.xxx");

    A something;
    while (input.good())
    {
        something.process(input);
    }

    return 0;
}

ifstream does not have a copy constructor. A(std::ifstream input) means "constructor for A taking an ifstream by value." That requires the compiler to make a copy of the stream to pass to the constructor, which it can't do because no such operation exists.

You need to pass the stream by reference (meaning, "use the same stream object, not a copy of it.") So change the constructor signature to A(std::ifstream& input). Note the ampersand, which means "reference" and, in the case of function parameters, means "pass this parameter by reference rather than by value.


Stylistic note: The body of your while loop, A(input);, constructs a structure of type A, which is then almost immediately destroyed when the while loop loops. Are you sure this is what you want to do? If this code is complete, then it would make more sense to make this a function, or a member function of A that is constructed outside the loop:

static void process(std::istream& stream)
{
    // some actions
    // note stream is declared as std::istream&; this lets you pass
    // streams that are *not* file-based, if you need to
}

int main()
{
    std::ifstream input("somefile.xxx");

    while (input.good())
    {
        process(input);
    }

    return 0;
}

OR

struct A
{   
    A()
    {
        // default constructor for struct A
    }

    void process(std::istream& stream)
    {
        // some actions
    }
};

int main()
{
    std::ifstream input("somefile.xxx");

    A something;
    while (input.good())
    {
        something.process(input);
    }

    return 0;
}
呆头 2024-10-02 16:59:35

两个错误:

  • ifstream 不可复制(将构造函数参数更改为引用)。
  • A(input); 相当于 A input;。因此编译器尝试调用默认构造函数。将其括起来 (A(input));。或者只是给它一个名称A a(input);

另外,为此使用函数有什么问题吗?似乎只使用了类的构造函数,您似乎将其滥用为返回 void 的函数。

Two bugs:

  • ifstream is not copyable (change the constructor parameter to a reference).
  • A(input); is equivalent to A input;. Thus the compiler tries to call the default constructor. Wrap parens around it (A(input));. Or just give it a name A a(input);.

Also, what's wrong with using a function for this? Only the class's constructor is used it seems, which you seem to abuse as a function returning void.

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