小编dar*_*van的帖子

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

我有这个代码的问题:

#include <fstream>

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

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

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

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

将其更改为此后编译(但这不能解决问题):

#include <fstream>

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

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

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

    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ constructor g++ ifstream

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

constructor ×1

g++ ×1

ifstream ×1