Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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.

like image 629
Juanpe Araque Avatar asked Aug 02 '26 22:08

Juanpe Araque


1 Answers

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.

like image 148
Borgleader Avatar answered Aug 04 '26 14:08

Borgleader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!