简体   繁体   English

将ifstream作为参数传递给Class Constructor

[英]Passing ifstream as an argument to Class Constructor

I am trying to pass: ifstream infile; 我试图通过:ifstream infile;

in my main (), to the constructor of a class called "FIFO": FIFO (infile); 在我的main()中,创建一个名为“ FIFO”的类的构造函数:FIFO(infile);

In the header file of FIFO (FIFO.h), I have: 在FIFO(FIFO.h)的头文件中,我有:

FIFO (std::ifstream);
std::ifstream infile;

And in FIFO.cc, I have: 在FIFO.cc中,我有:

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 在/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,来自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:错误:“ std :: ios_base&std :: ios_base :: operator =(const std :: ios_base&)”是私有的/ usr / lib / gcc / x86_64- redhat-linux / 4.4.7 /../../../../ include / c ++ / 4.4.7 / iosfwd:47:错误:在此上下文中

I am not sure if passing ifstream using reference works or not, or it is my codes problem. 我不确定使用引用传递ifstream是否有效,或者这是我的代码问题。

Thanks! 谢谢!

In your constructors declaration you are taking the std::ifstream by value instead of by reference. 在构造函数声明中,您按值而不是按引用获取std::ifstream 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. 并且您通过值而不是通过引用存储成员变量“ infile”。

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. 由于您现在要存储对ifstream的引用,因此需要在初始值设定项列表中对其进行初始化,而不是在构造函数中进行分配。

FIFO::FIFO (std::ifstream & INFILE)
    : infile(INFILE)
{
}

This is because the copy constructor of std::ifstream is private (or deleted in C++11). 这是因为std::ifstream的副本构造函数是私有的(或在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. 通过按值存储成员变量,您尝试制作传递给构造函数的std::ifstream对象的副本。

The signatures of your prototype declaration and implementation do not match. 原型声明和实现的签名不匹配。 Change 更改

FIFO (std::ifstream);

to

FIFO (std::ifstream &);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM