2012-02-20 70 views
2

我正在開發代理建模項目,並決定使用該工具。 我已經預先安裝了一堆庫,並下載了重新啓動源,並試圖將其包含在項目中。但突然出現我無法理解的錯誤。C++編譯錯誤(REPAST庫)

error: no match for ‘operator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator](((const char*)"_")) + boost::filesystem3::path::filename() const()’

CODE:

NCDataSet::NCDataSet(std::string file, const Schedule& schedule) : 
file_(file), schedule_(&schedule), start(0), open(true) 
{ 
    rank = RepastProcess::instance()->rank(); 
    if (rank == 0) { 
     fs::path filepath(file); 
     if (!fs::exists(filepath.parent_path())) { 
      fs::create_directories(filepath.parent_path()); 
     } else if (fs::exists(filepath)) { 
      string ts; 
      repast::timestamp2(ts); 
      fs::path to(filepath.parent_path()/(ts + "_" + filepath.filename())); 
     fs::rename(filepath, to); 
    } 
} 
} 
ERROR LINE: fs::path to(filepath.parent_path()/(ts + "_" + filepath.filename())); 

謝謝!

+0

是唯一的錯誤?如果沒有,你可以發佈完整的編譯器輸出嗎? – hmjd 2012-02-20 22:03:17

回答

1

該錯誤表明它不能匹配operator+,即你試圖附加兩個無效類型。

它看起來像path::filename不會返回一個std :: string。

class path { 
    // ... 
    path filename() const; 
    // ... 
}; 

認爲中綴操作符保持操作左側的類型是合理的。在這種情況下,std::string不知道任何關於提升或filesystem::path

所以你可能需要的那一行改變這樣的事情:

fs::path to(filepath.parent_path()/(ts + "_" + filepath.filename().string())); 

我發現時,它不是立即明顯如何一堆直列操作都導致錯誤,這是一個很好的做法,將所有內容分離到自己的行中。在這種情況下,它甚至會使您的代碼更清晰一些。

std::string old_filename(filepath.filename().string()); 
std::string new_filename = ts +"_"+ old_filename; 
fs::path to(filepath.parent_path()/new_filename);