The compiler returns a "synthesized method" operator = first required here "

I know this may be a simple question, but I have been working on it for the past hour and a half, and I am really lost.

Here's 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; }; 

A file is an object that I created, it is nothing more than a simple interface for working with an IO file. I have already done a vector of a custom object in the past, but this is 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 work under linux with gcc.

Any hints or tips as to which solution can be appreciated.

Sorry for the long post.

thanks

+6
source share
2 answers

In C ++ 03, std::vector<T> requires T to be copyable and a copy destination. File contains standard stream data elements, and standard streams are not copied, therefore, therefore, File also.

Your code will work fine in C ++ 11 (with move-construction / move-assign), but you need to avoid placing standard stream objects by value as data elements in C ++ 03. I recommend updating your compiler to those supports C ++ 11 move semantics or using one of Boost smart pointers .

+9
source

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

 _files.push_back( tempFile ); 

requires File have an open copy constructor. Since you provided other constructors, you should also provide this. The compiler does not synthesize it.

+2
source

Source: https://habr.com/ru/post/914070/


All Articles