c++ - 在类中使用 fstream 时出现问题

标签 c++ fstream

编译时出现以下错误:

1>c:\users\ra\source\repos\sandbox\game\gamesetup_1\gamesetup_1\main.cpp(15): error C2280: 'DebugLib::DebugLib(const DebugLib &)': attempting to reference a deleted function
1>c:\users\ra\source\commonincludes\tannic\debuglib\debuglib.h(41): note: compiler has generated 'DebugLib::DebugLib' here
1>c:\users\ra\source\commonincludes\tannic\debuglib\debuglib.h(41): note: 'DebugLib::DebugLib(const DebugLib &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::basic_fstream<char,std::char_traits<char>>::basic_fstream(const std::basic_fstream<char,std::char_traits<char>> &)'
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\fstream(1421): note: 'std::basic_fstream<char,std::char_traits<char>>::basic_fstream(const std::basic_fstream<char,std::char_traits<char>> &)': function was explicitly deleted
1>Done building project "GameSetup_1.vcxproj" -- FAILED.

代码如下所示:

调试Lib.h:

#include <string>
#include <fstream>

class DebugLib
{
public:
    DebugLib();             // Reset timestamp etc. 
    ~DebugLib();            // Flush output buffer
    void Init(uint8_t output, std::string fileName = "debug.log");  // Initializes Log
    void Log(int category, std::string msg);    // Add a line to the log
    void Flush();           // Output the remains of the Debug buffer
    void Shutdown();        // Shut it down

private:
    unsigned int m_initTime;

    unsigned int m_bufferPos;
    std::string m_outputBuffer[DEBUG_MAXSIZE];

    std::fstream m_fileStream;

    uint8_t m_output;
    bool m_running;
};

main.cpp:

#include <iostream>
#include <DebugLib.h>

using namespace std;

int main()
{
    DebugLib gDebugger = DebugLib();

    gDebugger.Init(DEBUG_LOG_TO_SCREEN);

    cout << "Running!" << endl;
    gDebugger.Shutdown(); 
    cin.get();
    return 0;
}

一旦我声明m_fileStream,我就会收到错误。我的声明有误吗? 当我在 DebugLib.cpp 中删除所有对 m_fileStream 的使用时,代码可以正常编译并运行(但当然不是按预期运行)

最佳答案

即使我之前见过这个问题,我也找不到重复的内容,所以:

让我们首先解释一下错误消息。我将忽略行号和错误代码,因为在您理解(或至少阅读)错误消息的其余部分之前,它们很少有用。

'DebugLib::DebugLib(const DebugLib &)': attempting to reference a deleted function

这是主要错误:尝试使用已删除的函数,即 DebugLib 的复制构造函数。由于您没有显式指定复制构造函数,因此由编译器为您定义一个复制构造函数。如果可能的话,编译器将定义一个简单的拷贝。如果这个定义不可能,它会为你删除复制构造函数。

正如您所注意到的,编译器能够定义一个简单的拷贝,直到您添加无法复制的字段(例如 std::fstream)。

note: compiler has generated 'DebugLib::DebugLib' here

这是一个澄清注释,有助于错误引用程序中的两行。主要错误消息附带的行号是您尝试进行复制的位置,此注释附带的行号是生成复制构造函数的位置。编译器试图提供帮助,因为它不知道您需要更改哪个位置来解决此错误。

note: 'DebugLib::DebugLib(const DebugLib &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::basic_fstream<char,std::char_traits<char>>::basic_fstream(const std::basic_fstream<char,std::char_traits<char>> &)'

此注释解释了您注意到的事情:由于无法复制 std::fstream 成员,因此无法复制您的类。此消息此时使用名称 basic_fstream,因此有助于了解 fstreambasic_fstream 模板的实例。因此,本注释末尾的困惑代码只是命名了 std::fstream 的复制构造函数。

note: 'std::basic_fstream<char,std::char_traits<char>>::basic_fstream(const std::basic_fstream<char,std::char_traits<char>> &)': function was explicitly deleted

这是进一步的澄清。在此之前的行表示“已删除或无法访问”。此行澄清了“显式删除”。

现在我们已经阅读了错误,我们可以查看它引用的行。麻烦的一行是

  DebugLib gDebugger = DebugLib();

此行请求默认构造 DebugLib 对象,然后将其复制到 gDebugger。但问题是:它无法复制!解决方案是通过删除拷贝来简化逻辑。您可以直接在 gDebugger 上调用默认构造函数。 (如果您的代码需要它们,这也适用于其他构造函数。)

    DebugLib gDebugger{};

作为奖励,您的代码更短。

关于c++ - 在类中使用 fstream 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562775/

相关文章:

c++ - 在C++中传递链接列表而不会导致内存泄漏

c++ - directx 上的一切都消失了

c++ - 为字符串值实现 cdbpp 库

c++ - 对 fstream 和指针感到困惑

c++ - fstream 指针未指向

c++ - boost::shared_ptr boost::mutex 和复制构造函数

c++ - 从模板基类派生时找不到类型

c++ - 变长数组 : How to create a buffer with variable size in C++

c++ - cpp字节文件读取

c++ - 从文本文件读取到数组