首页 > 解决方案 > 在课堂上使用 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.

代码如下所示:

调试库.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;
};

主.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我得到错误。我有错误的声明吗?当我删除 in 的所有使用时m_fileStreamDebugLib.cpp代码可以正常编译并运行(但当然不是参加的)

标签: c++fstream

解决方案


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

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

'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{};

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


推荐阅读