在类中使用fstream的麻烦 [英] Troubles using fstream in a class

查看:218
本文介绍了在类中使用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.

代码如下:

DebugLib.h:

DebugLib.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的使用时,代码可以正常编译并运行(但当然不会照看)

As soon as I declare m_fileStream I get the error. Do I have a wrong declaration ? When I remove all the use of m_fileStream in DebugLib.cpp, the code compiles fine, and runs (but of course not as attended)

推荐答案

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

让我们从解释错误消息开始.我将忽略行号和错误代码,因为这些行号和错误代码在您了解(或至少阅读)错误消息的其余部分后才非常有用.

Let's start by explaining the error messages. I'll ignore the line numbers and error codes, as those are rarely useful until after you've understood (or at least read) the rest of the error message.

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

这是主要错误:尝试使用已删除的函数,即DebugLib的副本构造函数.由于您没有显式指定副本构造函数,因此由编译器为您定义一个副本构造函数.如果可能,编译器将定义一个天真副本.如果无法定义,它将为您删除副本构造函数.

This is the main error: an attempt to use a function that is deleted, namely the copy constructor for DebugLib. Since you did not explicitly specify a copy constructor, it is up to the compiler to define one for you. The compiler will define a naive copy if possible. If this definition is not possible, it will delete the copy constructor for you.

正如您所注意到的,在您添加无法复制的字段(例如std::fstream)之前,编译器可以定义纯文本副本.

As you noticed, the compiler is able to define a naive copy until you add a field that cannot be copied (such as std::fstream).

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

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

This is a clarifying note that helps the error refer to two lines in your program. The line number that came with the main error message is where you tried to do the copy, and the line number that comes with this note is where the copy constructor is generated. The compiler is trying to be helpful because it doesn't know which location you'll want to change to address this error.

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的副本构造函数命名为.

This note explains the thing you noticed: copying your class is prevented because the std::fstream member cannot be copied. This message uses the name basic_fstream at this point, so it helps to know that fstream is an instantiation of the basic_fstream template. So that mess of code at the end of this note just names the copy constructor of 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

这是进一步的说明.在此之前的行表示已删除或无法访问".此行将其澄清为被明确删除".

This is a further clarification. The line before this said "deleted or inaccessible". This line clarifies that to "explicitly deleted".

现在我们已经阅读了该错误,我们可以看看它所指的行.麻烦的是

Now that we have read the error, we can go look at the lines to which it refers. The troublesome line is

  DebugLib gDebugger = DebugLib();

此行要求默认构造DebugLib对象,然后将其复制到gDebugger.还有一个问题:它不能被复制!解决方案是通过删除副本来简化逻辑.您可以直接在gDebugger上调用默认构造函数. (这也适用于其他构造函数,如果您的代码需要它们的话.)

This line requests that a DebugLib object be default constructed then copied to gDebugger. And there's the problem: it cannot be copied! The solution is to simplify your logic by removing the copy. You can invoked the default constructor directly on gDebugger. (This works for other constructors as well, should your code need them.)

    DebugLib gDebugger{};

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

As a bonus, your code is shorter.

这篇关于在类中使用fstream的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆