[英]Error C2248:cannot access private member declared in class
I have creates this class: 我创建了这个课:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class XLSCreator
{
private:
ofstream filecontents;
int fileType;
int cellNumber;
int rowsNumber;
public:
XLSCreator(string);
~XLSCreator();
void createType1File(string, string, string);
void createType2File(string, string, string, string, string);
void addNewRow();
void addData(string);
void saveFile();
};
But When I tried to build it, it gave me this error: 但是当我尝试构建它时,它给了我这个错误:
Error 2 error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>' 错误2错误C2248:'std :: basic_ofstream <_Elem,_Traits> :: basic_ofstream':无法访问在类'std :: basic_ofstream <_Elem,_Traits>中声明的私有成员
So can anybody help me with this? 那么有人可以帮我吗?
The error you describe could not have come from the code you have provided; 您描述的错误可能并非来自您提供的代码; your class declaration is fine.
您的课程声明就可以了。 It must have come from a different part of your code, in which you attempted to copy your
XLSCreator
class, eg by invoking copy-assignment: 它必须来自代码的另一部分,您尝试在其中复制
XLSCreator
类,例如,通过调用copy-assignment:
XLSCreator foo("filename");
XLSCreator bar = foo; //copies foo to create bar
The error arises when C++ attempts to generate a default copy-assignment operator for your class, which simply calls the copy-assignment operator of each member you've declared. 当C ++尝试为您的类生成默认的复制分配运算符时 ,会发生该错误,该操作仅调用您声明的每个成员的复制赋值运算符。
std::ofstream
is non-copyable, as @chris said in the comments, so when the compiler attempts to call the copy assignment operator of filecontents
, it finds that it's not allowed to ( operator=
is private within basic_ofstream
). 正如@chris在评论中所说,
std::ofstream
是不可复制的,因此,当编译器尝试调用filecontents
的副本分配运算符时,它会发现它是不允许的( operator=
在basic_ofstream
是private)。
In order to fix this, you either need to ensure you never copy an XLSCreator
, or explicitly define a copy-assignment operator for XLSCreator
that somehow handles the filecontents
member without attempting to copy it. 为了解决此问题,您要么需要确保从不复制
XLSCreator
,要么为XLSCreator
明确定义一个复制分配运算符,该运算符XLSCreator
某种方式处理filecontents
成员而不尝试对其进行复制。
Don't you need to import ofstream? 您不需要导入流吗? I don't think you can create ofstreams with just iostream and fstream.
我认为您不能仅使用iostream和fstream来创建ofstreams。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.