将ifstream作为参数传递给Class Constructor [英] Passing ifstream as an argument to Class Constructor
问题描述
在我的main()中,被称为FIFO的类的构造函数:FIFO(infile);
在FIFO(FIFO.h)的头文件中,我有:
FIFO(std :: ifstream);
std :: ifstream infile;
在FIFO.cc中,我有:
FIFO :: FIFO(std :: ifstream& INFILE)
{
infile = INFILE;
}
我不断变得喜欢(有更多的,我只是粘贴一个他们):
在/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../中包含的文件中/include/c++/4.4.7/bits/localefwd.h:43,
来自/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../ .. /include/c++/4.4.7/string:45,
from FIFO.cc:7:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../ .. /../../include/c++/4.4.7/bits/ios_base.h:在成员函数'std :: basic_ios>& std :: basic_ios> :: operator =(const std :: basic_ios>&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../ .. /../include/c++/4.4.7/bits/ios_base.h:793:error:'std :: ios_base& std :: ios_base :: operator =(const std :: ios_base&)'是private
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../。 ./include/c++/4.4.7/iosfwd:47:错误:在此上下文中
我不知道如果传递ifstream使用引用是否工作,我的代码问题。
谢谢!
在构造函数声明中, std :: ifstream
按值而不是引用。而不是
FIFO(std :: ifstream);
应为
FIFO(std :: ifstream&);
,并且通过值而不是引用存储成员变量'infile'。
std :: ifstream infile;
应为
std :: ifstream&英语:infile;
由于您现在存储对ifstream的引用,因此您需要在初始化程序列表而不是在构造函数中分配。
FIFO :: FIFO(std :: ifstream& INFILE)
:infile(INFILE)
{
}
因为 std :: ifstream
的复制构造函数是私有的(或在C ++ 11中删除)。通过按值存储成员变量,您试图复制传递给构造函数的 std :: ifstream
对象。
I am trying to pass: ifstream infile;
in my main (), to the constructor of a class called "FIFO": FIFO (infile);
In the header file of FIFO (FIFO.h), I have:
FIFO (std::ifstream);
std::ifstream infile;
And in FIFO.cc, I have:
FIFO::FIFO (std::ifstream & INFILE)
{
infile = INFILE;
}
I kept getting like (There are more of them, I just paste one of them):
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/localefwd.h:43, from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/string:45, from FIFO.cc:7: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h: In member function ‘std::basic_ios >& std::basic_ios >::operator=(const std::basic_ios >&)’: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h:793: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:47: error: within this context
I am not sure if passing ifstream using reference works or not, or it is my codes problem.
Thanks!
In your constructors declaration you are taking the std::ifstream
by value instead of by reference. Instead of
FIFO (std::ifstream);
it should be
FIFO (std::ifstream&);
and you are storing the member variable 'infile' by value instead of by reference.
std::ifstream infile;
should be
std::ifstream& infile;
Since you are now storing a reference to the ifstream you need to initialize it in the initializer list instead of assigning it in the constructor.
FIFO::FIFO (std::ifstream & INFILE)
: infile(INFILE)
{
}
This is because the copy constructor of std::ifstream
is private (or deleted in C++11). By storing the member variable by value you are attempting to make a copy of the std::ifstream
object passed to the constructor.
这篇关于将ifstream作为参数传递给Class Constructor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!