我正在创建一个用 C++ 编写文件的类,到目前为止我有这个代码,
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
class FileWriter
{
private:
bool isLittleEndian;
ofstream file;
public:
FileWriter(string fileName) : file("data.bin", ios::out | ios::binary)
{
int i = 1;
char *p = (char *)&i;
if(p[0] == 1)
isLittleEndian = true;
}
void writeByte()
{
}
void writeShort()
{
}
void writeInt()
{
}
void writeLong()
{
}
void writeUnsignedByte()
{
}
void writeUnsignedShort()
{
}
void writeUnsignedInt()
{
}
void writeUnsignedLong()
{
}
void writeFloat()
{
}
void writeDouble()
{
}
void writeString()
{
}
void closeFile()
{
file.close();
}
};
int main()
{
FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin");
writer.closeFile();
return 0;
}
但由于某种原因,它不会让我有一个 ofstream 字段,当我尝试它时说,Error 1 error C2248: 'std::basic_ofstream<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>' c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 25 1
我需要这个,因为我的类中的函数需要操纵这个流。我不明白为什么这很难做到。