Reputation: 1
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!
Upvotes: 0
Views: 7326
Reputation: 20063
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.
Upvotes: 13
Reputation: 96810
The signatures of your prototype declaration and implementation do not match. Change
FIFO (std::ifstream);
to
FIFO (std::ifstream &);
Upvotes: 0