简体   繁体   English

无法在C ++中将ofstream用作字段

[英]Cant use ofstream as field in c++

I am creating a class for writing file in c++, and i have this code so far, 我正在创建一个用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;
}

but for some reason, it wont let me have an ofstream field, and when i try it says, 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 I need this because functions in my class need to manipulate this stream. 但由于某种原因,它不会让我有一个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我需要这个,因为我班上的函数需要操纵这个流。 I don't see why this so hard to do. 我不明白为什么这么难做到。

You can't copy or assign an std::ofstream , which means you can't do this: 您不能复制或分配std::ofstream ,这意味着您不能这样做:

file = openedFile;

You need to either initialize it correctly, or move-copy-assign. 您需要正确初始化它,或者移动副本分配。

Initialization (preferred option): 初始化(首选选项):

FileWriter(string fileName) : file("data.bin", ios::out | ios::binary)
{ 
  ...
}

Move-copy assignment: 移动副本分配:

file = std::move(openedFile);

Alternatively, you can use the std::ofstream::open method: 另外,您可以使用std::ofstream::open方法:

file.open("data.bin", ios::out | ios::binary);

You can just open the existing object: 您可以打开现有对象:

file.open("data.bin", ios::out | ios::binary);

And you need to avoid calling (or at least requiring) the copy-ctor of your class. 而且您需要避免调用(或至少要求)类的copy-ctor。 Change 更改

FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin");

to: 至:

FileWriter writer("C:\\Users\\Owner\\Desktop\\Test.bin");
file = openedFile;

There is only one operator= defined for basic_ofstream , the move-assignment operator. basic_ofstream定义了一个operator= ,即移动分配操作符。 The copy-assignment operator is implicitly deleted, and so this code will not work. 复制分配运算符被隐式删除,因此此代码将不起作用。

You need to use the member-initializer list to initialize file with the path: 您需要使用member-initializer列表使用以下路径初始化file

FileWriter(std::string fileName)
    : file(fileName, ios::out | ios::binary)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM