c++ - 隐式删除的复制构造函数编译错误返回指针的值

标签 c++

很抱歉,如果这个问题已经得到解答,但我找不到任何可能的修复方法。

考虑这个类

     class NPALog{
public:
    NPALog();
    ~NPALog();

    void error(char* message);
    void warning(char* message);
    void log(char* message);

    void setOutput(char* fileName);
    std::ofstream getLogBuffer(){return *m_logOutputFile;};
    std::ofstream getErrorBuffer(){return *m_errorOutputFile;};


private:
    char* m_fileName;
    std::ofstream *m_logOutputFile;
    std::ofstream *m_errorOutputFile;

 };

当我尝试编译它时,getLogBuffer 函数中出现此错误:

call to implicitly-deleted copy constructor of 'std::ofstream' (aka 'basic_ofstream<char>')

我不太了解复制构造函数,但我唯一想做的就是使用指针,这些指针允许我稍后轻松定义每个流,并在用户想要使用缓冲区时返回缓冲区本身。

你知道这里的问题是什么吗?关于如何做得更好的任何想法?

非常感谢。

最佳答案

您将通过 getLogBuffer()getErrorBuffer() 的值返回 std::ofstream,这将调用拷贝ctor 哪个(如错误消息所示)已被删除。您应该返回一个引用。

关于c++ - 隐式删除的复制构造函数编译错误返回指针的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122873/

相关文章:

c++ - 线程池(大概)锁定条件变量和互斥量的问题

c++ - 使用简单宏 C++ 的未声明标识符错误

c++ - 为什么我需要在字符串前加上一个 L 并且我是否正确地创建了这个变量?

c++ - 两个整数双端队列的乘法奇怪的错误

c++ - cin.get() 和 cin.getline() 的区别

c++ - 在什么情况下 ref_view{E} 格式错误而 subrange{E} 不是?

c++ - 使用 malloc/new 时无法编译 stm32l4r5xx

c++ - 为什么 std::variant 在 GCC 8.5 和 GCC 12.1 上对于 `const char *` 文字的行为不同?

C++将一个char数组的一部分复制到另一个char数组

c++ - 为什么可以从函数返回 'vector'?