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.
代码如下所示:
调试库.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_fileStream
,DebugLib.cpp
代码可以正常编译并运行(但当然不是参加的)
解决方案
即使我以前见过这个问题,我也找不到重复项,所以:
让我们从解释错误消息开始。我将忽略行号和错误代码,因为在您理解(或至少阅读)错误消息的其余部分之前,它们很少有用。
'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
此时使用该名称,因此有助于了解这fstream
是basic_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{};
作为奖励,您的代码更短。
推荐阅读
- ios - IOS/Storyboard:图像在长时间重做后从身份检查器中消失,但在 Storyboard 中仍然可见
- react-native - 在 react native 中链接只能打开一个应用
- python - 具有子数组和可变长度的 numpy 关联数组的最佳生成
- java - 生物特征认证java应用程序。如何在智能手机和应用程序之间建立连接
- redux - mapDispatchToProps 函数未定义
- python-3.x - 在 Masonite 中在哪里更改 HTTP 端口设置?
- aggregate - R中每天的日夜平均值
- objective-c - 从 HTMLKit 中的 HTMLDocument 获取 href
- c# - 我如何制作这个控制台应用程序
- java - 我的方法不适用于参数