[英]Can't push_back into vector - gives no overload error
我看了過去的幾個問題(例如, 沒有重載函數的實例 ),但是這些都不相關。 我知道類型不匹配,但是我不明白為什么在我的設置中出現此錯誤。
我收到此錯誤:
沒有重載函數“ std :: vector <_Tp,_Alloc> :: push_back [with _Tp = int,_Alloc = std :: allocator <int>]”的實例與參數列表和對象匹配(該對象具有防止使用匹配)-參數類型為:(int)-對象類型為:const std :: vector <int,std :: allocator <int >>
這是代碼:
std::vector<int> sorted_edges;
...
//let's sort the edges
for(int i = 0; i < num_nodes; ++i){
for(int j = 0; j < num_nodes; ++j){
if(graph[i][j] != INF){
sorted_edges.push_back(i);
}
}
}
注意:我不會將int
推入sorted_edges
我正在測試以查看是否錯誤地創建了邊緣結構,或者是否錯誤地使用了向量。
關於錯誤您得到:
...對象具有防止匹配的類型限定符 -參數類型為:(int)-對象類型為: const std :: vector。
首先,您應該檢查發布的代碼是否正確-您聲明它是非常量,但錯誤明確指出否則,盡管您可能會將其作為const ref傳遞給函數-這是一種可能。
無論如何,您不能將push_back
推入const vector
因為它是const
:-)
您可以通過以下代碼看到它:
#include <vector>
int main() { XYZZY std::vector<int> x; x.push_back(42); }
當您使用-DXYZZY=
編譯(這樣XYZZY
有效消失)時,它可以編譯。 但是,如果使用-DXYZZY=const
,則會出現錯誤:
qq.cpp: In function ‘int main()’:
qq.cpp:2:54: error: passing ‘const std::vector<int>’ as ‘this’
argument discards qualifiers [-fpermissive]
int main() { XYZZY std::vector<int> x; x.push_back(42); }
^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.