2012-04-24 13 views
6

我知道這可能是一個簡單的問題,但我一直在努力在過去的一個半小時,我真的失去了。編譯器返回「合成方法‘運算符=’在這裏首先需要」

在這裏,編譯器錯誤:

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對象的代碼: 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下工作與海灣合作委員會。

任何提示或提示,以什麼解決方案,可以理解。

很抱歉的長期職位。

感謝

回答

9

在C++ 03,std::vector<T>要求T被複制施工的複製和可分配。 File包含標準流數據成員,以及標準流是不可複製的,所以結果是File爲好。

你的代碼將在工作的罰款C++ 11(含布展施工/布展分配),但你需要避免的值在C++ 03的數據成員保持標準流對象。我建議將您的編譯器升級到支持C++ 11移動語義或使用Boost's smart pointers之一的編譯器。

2

我不知道該錯誤消息,但此行:

_files.push_back(tempFile); 

要求File有一個公共的拷貝構造函數。既然你提供了其他的構造函數,你也必須提供這個。編譯器不合成它。