很抱歉,如果这个问题已经得到解答,但我找不到任何可能的修复方法。
考虑这个类
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/