我在C++程序(它处理I/O流)中编程我的第一个大“类”,我想我理解了对象,方法和属性的概念。 虽然我想我还没有得到封装概念的所有权利, 因为我希望我的类名为文件有在其他类中声明的私有成员
- 名称(文件的路径),
- 读取流和
- 书写流
的属性,
也是它的第一种方法实际得到的的“写流”属性File对象...
#include <string>
#include <fstream>
class File {
public:
File(const char path[]) : m_filePath(path) {}; // Constructor
File(std::string path) : m_filePath(path) {}; // Constructor overloaded
~File(); // Destructor
static std::ofstream getOfstream(){ // will get the private parameter std::ofStream of the object File
return m_fileOStream;
};
private:
std::string m_filePath; // const char *m_filePath[]
std::ofstream m_fileOStream;
std::ifstream m_fileIStream;
};
但我得到的错误:
Error 4 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream 1116
报告我fstream.cc的以下部分:
private:
_Myfb _Filebuffer; // the file buffer
};
你能不能再帮我解决这和能够使用流作为我的课的参数吗?我试图返回一个引用而不是流本身,但是我也需要一些帮助(也不管用......)。 在此先感谢
您应该在您尝试从静态成员函数访问实例成员的事实中出现错误。当你把它称为'File :: getOfstream()'时会发生什么? – chris 2012-07-24 18:20:29
也许你应该考虑将*引用*返回给std :: ofstream而不是副本? – Roddy 2012-07-24 18:21:30
这真的是第一个错误信息吗?我会期待一些(可能是隐含的)复制构造函数。 – aschepler 2012-07-24 18:21:45