2017-02-11 51 views
0

在這個片段中,矢量<bool>沒有可行的重載 '='

void Graph::dfs(int s) const { 
    m_Marked[s] = true; 
    // ... 
} 

其中std::vector<bool> m_Marked;

我從編譯器看到,

../Algorithms/graph.cpp:54:17: error: no viable overloaded '=' 
    m_Marked[s] = true; 
    ~~~~~~~~~~~^~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:155:28: note: candidate function not viable: no known conversion from 'bool' to 'const std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >' for 1st argument 
    __bit_const_reference& operator=(const __bit_const_reference& __x); 

完全不知道他的問題是什麼,這似乎很基本?

TIA

+3

從對象方法中刪除'const'。 或者,但不建議,將'm_Marked'聲明爲'mutable'。 –

回答

5

您將函數定義爲const,因此您承諾不會更改任何內容。分配操作正在改變某些事情,並違反了這一承諾。

相關問題