简体   繁体   中英

Implicitly-deleted copy constructor compile error returning value of a pointer

sorry if this has already been answered but I couldn't find anything with a possible fix to it.

Consider this class

     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;

 };

When I try to compile it I have this error in the getLogBuffer function:

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

I didn't know too much about copy constructors but the only thing I want to do is work with pointers that allow me to define each ofstream easily later and return the buffer itself if the user want to use it.

Do you know what can be the problem here? Any idea on how to do it better?

Thanks a lot in advance.

You're returning std::ofstream by value from getLogBuffer() and getErrorBuffer() which will make a call to the copy ctor which (as the error message suggests) has been deleted. You should return a reference instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM