![](/img/trans.png)
[英]why “istream& object” requires reference & and ifstream doesn't?
[英]ifstream return object by reference
//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
}
嗨,我有以上課程結構
ifstream ifs1 = storeManager.getDataWriterStream();
我得到這個,請幫助我沒有得到這個,
/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
您正在嘗試復制-構造流,但無法完成。 您錯過了一個&
:
ifstream& ifs1 = storeManager.getDataWriterStream();
//------^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.