编译器返回“合成方法'operator='首先在这里需要”

7

我知道这可能是一个简单的问题,但我已经花了一个半小时在上面工作了,现在真的很迷茫。

这里是编译器错误:

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

File是我创建的一个对象,它仅仅是一个简单的文件IO接口。之前我已经处理过用户定义对象的向量,但这是我第一次遇到此错误。

以下是File对象的代码: 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 );
}

我正在使用gcc在Linux下工作。

如果有任何解决方案的提示或建议,都将不胜感激。

很抱歉我的帖子写得有些冗长。

谢谢。

2个回答

10
在C++03中,std::vector<T>要求T是可复制构造和可复制分配的。 File包含标准流数据成员,而标准流是不可复制的,因此File也是不可复制的。
您的代码在C++11中可以正常工作(使用移动构造/移动分配),但在C++03中需要避免按值持有标准流对象作为数据成员。我建议升级编译器以支持C++11移动语义或使用Boost的智能指针之一。

3

我不确定错误信息是什么,但是这一行:

_files.push_back( tempFile );

要求File有一个公共的复制构造函数。由于你已经提供了其他构造函数,因此你也必须提供这个。编译器不会合成它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接