Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler returns "synthesized method ‘operator=’ first required here"

Tags:

c++

gcc

I know it may be a simple question, but I've been working on it for the last hour and a half and I'm really lost.

Here the compiler error:

synthesized method ‘File& File::operator=(const File&)’ first required here 

I have this bit of code :

void FileManager::InitManager()
{
    int numberOfFile = Settings::GetSettings()->NumberOfFile() + 1;

    for( unsigned int i = 1; i < numberOfFile; i++ )
    {
        std::string path = "data/data" ;
        path += i;
        path += ".ndb";

        File tempFile( path );

        _files.push_back( tempFile ); // line that cause the error

        /*if( PRINT_LOAD )
        {
            std::cout << "Adding file " << path << std::endl;
        }*/
    }
}

_files if defined in this header :

#pragma once

//C++ Header
#include <vector>

//C Header

//local header
#include "file.h"

class FileManager
{
public:
    static FileManager* GetManager();
    ~FileManager();

    void LoadAllTitle();

private:
    FileManager();
    static FileManager* _fileManager;

    std::vector<File> _files;
};

File is an object I created, it is nothing more than a simple interface to deal with file IO. I have already done vector of user-defined object in the past, but it's the first time I get this error.

Here is the code for the File object: File.h

#pragma once

//C++ Header
#include <fstream>
#include <vector>
#include <string>

//C Header

//local header

class File
{
public:
    File();
    File( std::string path );
    ~File();

    std::string ReadTitle();

    void ReadContent();
    std::vector<std::string> GetContent();

private:
    std::ifstream _input;
    std::ofstream _output;

    char _IO;
    std::string _path;
    std::vector<std::string> _content;
};

File.cpp

#include "file.h"

File::File()
    : _path( "data/default.ndb" )
{
}

File::File( std::string path )
    : _path( path )
{
}

File::~File()
{
}

void File::ReadContent()
{
}

std::string File::ReadTitle()
{
    _input.open( _path.c_str() );
    std::string title = "";

    while( !_input.eof() )
    {
        std::string buffer;
        getline( _input, buffer );

        if( buffer.substr( 0, 5 ) == "title" )
        {
            title = buffer.substr( 6 ); // 5 + 1 = 6... since we want to skip the '=' in the ndb
        }
    }

    _input.close();
    return( title );
}

std::vector<std::string> File::GetContent()
{
    return( _content );
}

I'm working under linux with gcc.

Any hints or tips as to what the solution could be is appreciated.

Sorry for the long post.

Thanks

like image 510
TurnsCoffeeIntoScripts Avatar asked Apr 24 '12 20:04

TurnsCoffeeIntoScripts


2 Answers

In C++03, std::vector<T> requires that T be copy-constructable and copy-assignable. File contains standard stream data members, and standard streams are non-copyable, so consequently File is as well.

Your code would work fine in C++11 (with move-construction/move-assignment), but you'll need to avoid holding standard stream objects by value as data members in C++03. I recommend upgrading your compiler to one that supports C++11 move semantics or using one of Boost's smart pointers.

like image 152
ildjarn Avatar answered Nov 08 '22 19:11

ildjarn


I am not sure about the error message, but this line:

_files.push_back( tempFile );

requires that File have a public copy constructor. Since you have provided other constructors, you also have to provide this one. The compiler doesn't synthesize it.

like image 3
juanchopanza Avatar answered Nov 08 '22 19:11

juanchopanza