2011-06-10 55 views
1

我試圖使用從這個問題的解決方案:使用std ::複製 - 錯誤C2679:無法找到正確的二進制 '=' 操作

錯誤信息

C:\程序文件(86)\微軟的Visual Studio 10.0 \ VC \包括\ xutility(2144):錯誤C2679:二進制 '=':沒有操作員發現它接受一個右手OPE蘭特類型「const的線」的(或不存在可接受的轉化率)

(並在此之後一堆模板跟蹤數據)

我使用Visual C++ 2010速成。

代碼

#include<string> 
#include<iostream> 
#include<fstream> 
#include<vector> 
#include<iterator> 

class Line 
{ 
    std::string data; 

public: 
    friend std::istream& operator>>(std::istream& inputStream, Line& line) 
    { 
    std::getline(inputStream, line.data); 
    return inputStream; 
    } 

    operator std::string() 
    { 
    return data; 
    } 
}; 

int main(int argc, char* argv[]) 
{ 
    std::fstream file("filename.txt", std::fstream::in | std::fstream::out); 
    std::vector<std::string> lines; 

    // error is in one of these lines 
    std::copy(
    std::istream_iterator<Line>(file), 
    std::istream_iterator<Line>(), 
    std::back_inserter(lines)); 
} 
+0

還有一個類似的錯誤(我認爲)GCC:http://codepad.org/G3Chty9K – 2011-06-10 06:51:40

回答

2

下面是正確的版本,編譯罰款:

class Line 
{ 
    std::string data; 

    public: 
     friend std::istream& operator>>(std::istream& inputStream, Line& line) 
     { 
      std::getline(inputStream, line.data); 
      return inputStream; 
     } 

     operator std::string() const 
     { 
      return data; 
     } 
}; 

轉換操作符需要被const

2

我改變:

operator std::string() 

operator std::string() const 

,它編譯罰款。

相關問題