简体   繁体   English

错误C2248:无法访问在类中声明的私有成员

[英]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.

相关问题 错误C2248:无法访问类中声明的私有成员 - error C2248: cannot access private member declared in class C2248:无法访问类中声明的私有成员 - C2248: Cannot access private member declared in class 错误C2248:无法访问在类中声明的受保护成员 - error C2248: cannot access protected member declared in class 错误C2248:无法访问类中声明的受保护成员 - error C2248 : cannot access protected member declared in class CPtrList 无法使用:错误 C2248:“CObject::operator =”:无法访问类“CObject”中声明的私有成员 - CPtrList cannot use: error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' C ++错误C2248:无法访问在SUPER类中声明的私有成员 - C++ error C2248: cannot access private member declared in SUPER class 无法访问在类&#39;QReadWriteLock&#39;中声明的私有成员错误1错误C2248:&#39;QReadWriteLock :: QReadWriteLock&#39; - Cannot access private member declared in class 'QReadWriteLock'Error 1 error C2248: 'QReadWriteLock::QReadWriteLock' 错误C2248:“ Gdiplus :: Bitmap :: Bitmap”:无法访问在类“ Gdiplus :: Bitmap”中声明的私有成员 - error C2248: 'Gdiplus::Bitmap::Bitmap' : cannot access private member declared in class 'Gdiplus::Bitmap' 错误C2248:“ klientPracownik :: klientPracownik”:无法访问在类“ klientPracownik”中声明的私有成员 - error C2248: 'klientPracownik::klientPracownik' : cannot access private member declared in class 'klientPracownik' 错误C2248:&#39;CvSVM :: CvSVM&#39;:无法访问类&#39;CvSVM&#39;中声明的私有成员 - error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM