繁体   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