微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 编译器返回“合成方法’operator =’这里首先需要”

我知道这可能是一个简单的问题,但我在最后一个半小时一直在努力,我真的迷失了.

这里编译错误

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

我在linux下用gcc工作.

任何有关解决方案的提示提示都表示赞赏.

对不起,很长的帖子.

谢谢

解决方法

在C 03中,std :: vector< T>要求T是可复制构造和可复制分配的.文件包含标准流数据成员,标准流是不可复制的,因此File也是如此.

您的代码在C 11中可以正常工作(使用移动构造/移动分配),但是您需要避免将标准流对象按值保存为C 03中的数据成员.我建议将编译器升级支持C 11的编译器.移动语义或使用Boost’s smart pointers之一.

原文地址:https://www.jb51.cc/c/111605.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐