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;
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
流是不可复制的。
所以你需要通过引用传递。
Streams are non copyable.
So you need to pass by reference.
ifstream
没有复制构造函数。A(std::ifstream input)
表示“A
的构造函数,采用ifstream
按值”。这要求编译器制作流的副本以传递给构造函数,但它无法执行此操作,因为不存在此类操作。您需要通过引用传递流(意思是,“使用相同的流对象,而不是它的副本。”)因此,将构造函数签名更改为 A(std::ifstream& input)。请注意&符号,它的意思是“引用”,对于函数参数来说,意思是“通过引用传递此参数,而不是通过值。
风格注释:
while
循环的主体A(input);
构造了一个A
类型的结构,然后几乎立即当while
循环循环时被销毁。您确定这是您想要执行的操作吗?如果此代码完整,那么将其设为一个函数或的成员函数会更有意义。 >在循环外部构建的
:OR
ifstream
does not have a copy constructor.A(std::ifstream input)
means "constructor forA
taking anifstream
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 typeA
, which is then almost immediately destroyed when thewhile
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 ofA
that is constructed outside the loop:OR
两个错误:
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 toA input;
. Thus the compiler tries to call the default constructor. Wrap parens around it(A(input));
. Or just give it a nameA 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
.