簡體   English   中英

C ++從此處實例化錯誤,並帶有std :: set

[英]C++ instantiated from here error with std::set

我發現了一堆關於“從這里實例化”問題的線索。 他們似乎都是創建了忘記默認構造函數的人。 我認為我的問題是不同的(但是,我是C ++的新手,在同一問題上可能稍有不同,而我只是不知道如何實現該解決方案)。

我正在嘗試插入集合,顯然是從那里實例化的。 而且它拋出一個錯誤。

class Node{
public:
bool operator <(const Node& other){
    return id < other.id;
}
class Graph {
public:
    int poner;
    map<string, Node> nodeMap;
    set<Node> reachables;


    void DepthFirstSearch(Node node){
        reachables.clear(); //fine at this point
        poner = 0;
        DFS(node);
    }


    private:
        void DFS(Node node){
            reachables.insert(node); //instantiated from here
        }

    };


Node.h:131:25: instantiated from here
c:\..... errir: passing 'const Node' as 'this' argument of 'bool Node::operator<(const Node&)' discards qualifiers [-fpermissive]

任何幫助總是很感激。

在某個地方,有人試圖將const Nodeconst Node進行比較。 由於您的operator<未標記為const ,因此失敗。

operator<(const Node& other) const {}
                             ^^^^^ 

標准庫期望比較在邏輯上是const 如果它們確實不能為const ,請使用mutable隱藏操作員正在進行突變,但請確保從外部看不到該變量。

關於錯誤消息: instantiated from here實際上僅意味着這段代碼負責實例化發生錯誤的模板。 這不是真正的錯誤,而是實例化回溯的一部分。 真正的錯誤通常(在gcc中)包含在單詞error后面,聽起來可能很明顯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM