我正在尝试用一些简单的方法创建一个程序,这是我的一个类:
Example.cpp:
#include "Example.hpp"
using namespace std;
ifstream Example::getStream(const char *inputFile) const {
ifstream myFile;
myFile.open(inputFile);
return myFile;
}
Example.hpp:
#include <fstream>
#include <map>
#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP
using namespace std;
class Example {
public:
map<int, int> *exampleMap;
ifstream getStream(const char *inputFile) const;
};
#endif
这是由程序中的其他子类实现的,将来它将非常有用。但是,就目前而言,当我尝试构建它时,它会返回以下错误:
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/istream:38,
from /usr/include/c++/4.8/fstream:38,
from ../Example.hpp:1,
from ../Example.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.8/bits/ios_base.h:786:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
ios_base(const ios_base&);
^
In file included from /usr/include/c++/4.8/ios:44:0,
from /usr/include/c++/4.8/istream:38,
from /usr/include/c++/4.8/fstream:38,
from ../Example.hpp:1,
from ../Example.cpp:1:
/usr/include/c++/4.8/bits/basic_ios.h:66:11: error: within this context
class basic_ios : public ios_base
^
In file included from ../Example.hpp:1:0,
from ../Example.cpp:1:
/usr/include/c++/4.8/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/4.8/fstream:427:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
class basic_ifstream : public basic_istream<_CharT, _Traits>
^
In file included from /usr/include/c++/4.8/ios:43:0,
from /usr/include/c++/4.8/istream:38,
from /usr/include/c++/4.8/fstream:38,
from ../Example.hpp:1,
from ../Example.cpp:1:
/usr/include/c++/4.8/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/include/c++/4.8/streambuf:802:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
basic_streambuf(const basic_streambuf& __sb)
^
In file included from ../Example.hpp:1:0,
from ../Example.cpp:1:
/usr/include/c++/4.8/fstream:72:11: error: within this context
class basic_filebuf : public basic_streambuf<_CharT, _Traits>
^
/usr/include/c++/4.8/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/4.8/fstream:427:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here
class basic_ifstream : public basic_istream<_CharT, _Traits>
^
../Example.cpp: In member function ‘std::ifstream Example::getStream(const char*) const’:
../Example.cpp:13:12: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here
return myFile;
^
make: *** [Example.o] Error 1
我想弄清楚我在这里做错了什么。这是包含的内容吗?这是一些我看不到的拼写错误。
提前致谢。
答案 0 :(得分:4)
您正尝试按价值返回流。这在C ++ 11之前是不可能的(并且使用gcc 4.X的标准库实现),因为流不能被复制,而C ++ 98没有移动语义。
所以在C ++ 11中,这段代码很好;如果您的实现尚未完全实现它,则需要一种解决方法,例如传入非const
引用,例如:
#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP
#include <fstream>
#include <map>
class Example
{
public:
std::map<int, int> *exampleMap;
bool openFile(const char *inputFile, std::ifstream &myFile) const;
};
#endif
#include "Example.hpp"
bool Example::openFile(const char *inputFile, std::ifstream &s) const
{
myFile.open(inputFile);
return myFile.is_open();
}
#include "Example.hpp"
Example ex;
std::ifstream myFile;
if (ex.openFile("filename", myFile))
{
//...
}