6

簡単な質問かもしれませんが、私はこの問題に 1 時間半も取り組んできましたが、本当に途方に暮れています。

ここでコンパイラエラー:

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

私はこのビットのコードを持っています:

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 (このヘッダーで定義されている場合):

#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;
};

ファイルは私が作成したオブジェクトであり、ファイル IO を処理するための単純なインターフェイスにすぎません。過去にユーザー定義オブジェクトのベクトルを実行したことがありますが、このエラーが発生するのは初めてです。

File オブジェクトのコードは次のとおりです。

#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;
};

ファイル.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 );
}

Linuxでgccを使用しています。

解決策が何であるかについてのヒントやヒントをいただければ幸いです。

長い投稿で申し訳ありません。

ありがとう

4

2 に答える 2

9

C++03 では、コピー構築可能かつコピー代入可能であるstd::vector<T>必要があります。標準ストリーム データ メンバーが含まれており、標準ストリームはコピーできないため、同様にコピーできます。TFileFile

コードは C++11 (move-construction/move-assignment を使用) では問題なく動作しますが、C++03 では標準ストリーム オブジェクトを値によってデータ メンバーとして保持しないようにする必要があります。C++11 の移動セマンティクスをサポートするコンパイラにアップグレードするか、Boost のスマート ポインターのいずれかを使用することをお勧めします。

于 2012-04-24T20:25:58.873 に答える
2

エラーメッセージについてはわかりませんが、次の行:

_files.push_back( tempFile );

Fileパブリック コピー コンストラクターが必要です。他のコンストラクターを提供したので、これも提供する必要があります。コンパイラはそれを合成しません。

于 2012-04-24T20:25:32.527 に答える