1
//StoreManager.cpp
ifstream&  StoreManager::getDataReaderStream() 
{
        m_dataReader.open(m_dataReaderFileName.c_str(), ios::in | ios::binary);
        return m_dataReader; 
}
//multithread.cpp
void runThread (void *lpData) 
{       
 ifstream ifs1 = storeManager.getDataWriterStream();
 //other code 
}

Hi, I am having above class structure

ifstream ifs1 = storeManager.getDataWriterStream();

i am getting this please help i am not getting this,

/usr/include/c++/4.6/fstream: In copy constructor âstd::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)â:
/usr/include/c++/4.6/fstream:420:11: note: synthesized method âstd::basic_ios<char>::basic_ios(const std::basic_ios<char>&)â first required here
/usr/include/c++/4.6/streambuf: In copy constructor âstd::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)â:
/usr/include/c++/4.6/streambuf:782:7: error: âstd::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]â is private
/usr/include/c++/4.6/fstream:69:11: error: within this context
/usr/include/c++/4.6/fstream: In copy constructor âstd::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)â:
/usr/include/c++/4.6/fstream:420:11: note: synthesized method âstd::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)â first required here
multithread.cpp: In member function âvirtual void Multithread::runThread(void*)â:
multithread.cpp:33:57: note: synthesized method âstd::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)â first required here
Dipak
  • 123
  • 1
  • 1
  • 10

1 Answers1

5

You're trying to copy-construct the stream, which cannot be done. You have missed one &:

ifstream& ifs1 = storeManager.getDataWriterStream();
//------^
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • See this question/answer. http://stackoverflow.com/questions/6980719/c-getline-compile-error-when-called-from-function?rq=1 – Dwayne Towell Nov 27 '13 at 08:03
  • @DwayneTowell The OP's stream is a member of some class, it's not a local, so this will work. – Kiril Kirov Nov 27 '13 at 08:06
  • Right, but it does explain a bit more about why there is a problem rather than just thinking of it as a typo. – Dwayne Towell Nov 27 '13 at 08:13
  • ifstream& ifs1; ifs1 = storeManager.getDataWriterStream(); will it work for this – Dipak Nov 27 '13 at 08:14
  • @Dipak - no, you cannot leave a reference non-initialized and then later assign a "value" to it. If you _need_ this, use pointers: `ifstream* ifs1 = NULL; /* ... */ ifs1 = &storeManager.getDataWriterStream();` But be careful with the lifetime of `storageManager`. – Kiril Kirov Nov 27 '13 at 08:24