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>'
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 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. 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
).
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.
Don't you need to import ofstream? I don't think you can create ofstreams with just iostream and fstream.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.