c++ - 常量冲突

标签 c++ initialization constants return-value

我有以下类(class)

class node {
public:
    node() { }
    node(const node&);
    node(luint inID) { ID = inID; }
    ~node() { neighbors.clear(); }

    node& operator=(const node&);
    void addNeighbor(luint);
    void addNeighbor(const std::vector<luint>& );
    void setID(luint inID) { ID = inID; }
    luint getID() const { return ID; }
    std::vector<luint> & getNeighbors() const { return neighbors; }
protected:
    luint ID;
    std::vector<luint> neighbors;
};
void node::addNeighbor(const std::vector<luint>& inID) {
    for(int i = 0; i < inID.size(); i++)
        neighbors.push_back( inID[i] );
}
// etc..

现在我得到的错误是

graph.h: In member function 'std::vector<long unsigned int, std::allocator<long unsigned int> >& node::getNeighbors() const': In file included from main.cpp:10:
graph.h:28: error: invalid initialization of reference of type 'std::vector<long unsigned int, std::allocator<long unsigned int> >&' from expression of type 'const std::vector<long unsigned int, std::allocator<long unsigned int> >'
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

另一方面,如果我在函数“getNeighbors()”的声明中删除“const”,则会收到错误

graph.cpp: In member function 'void graph::addNode(const node*)':
graph.cpp:36: error: request for member 'addNeighbor' in '((graph*)this)->graph::nodeMap. std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = long unsigned int, _Tp = node*, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<std::pair<const long unsigned int, node*> >](((const long unsigned int&)((const long unsigned int*)(& inNode-> node::getID()))))', which is of non-class type 'node*'
graph.cpp:36: error: passing 'const node' as 'this' argument of 'std::vector<long unsigned int, std::allocator<long unsigned int> >& node::getNeighbors()' discards qualifiers
make[2]: *** [build/Debug/GNU-MacOSX/graph.o] Error 1

有人知道如何解决这个问题吗?

提前谢谢您,


更新:

class node {
public:
    node() { }
    node(const node&);
    node(luint inID) { ID = inID; }
    ~node() { neighbors.clear(); }

    node& operator=(const node&);
    void addNeighbor(luint);
    void addNeighbor(const std::vector<luint>& );
    void setID(luint inID) { ID = inID; }
    luint getID() const { return ID; }
    std::vector<luint> & getNeighbors() { return neighbors; }
    std::vector<luint> const & getNeighbors() const { return neighbors; }
protected:
    luint ID;
    std::vector<luint> neighbors;
};

class graph {
public:
    graph() { }
    ~graph() { }

    void addNode(const node*);
    void addNode(const node&);
protected:
    std::map<luint, node*> nodeMap;
};

void node::addNeighbor(luint inID) {
    neighbors.push_back(ID);
}

void node::addNeighbor(const std::vector<luint>& inID) {
    for(int i = 0; i < inID.size(); i++)
        neighbors.push_back( inID[i] );
}

void graph::addNode(const node* inNode) {
    nodeMap[inNode->getID()] = new node(inNode->getID());
    nodeMap[inNode->getID()].addNeighbor( inNode->getNeighbors() );
}

void graph::addNode(const node& inNode) {
    nodeMap[inNode.getID()] = new node(inNode.getID());
}

编译错误:

graph.cpp: In member function 'void graph::addNode(const node*)':
graph.cpp:36: error: request for member 'addNeighbor' in '((graph*)this)->graph::nodeMap. std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = long unsigned int, _Tp = node*, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<std::pair<const long unsigned int, node*> >](((const long unsigned int&)((const long unsigned int*)(& inNode-> node::getID()))))', which is of non-class type 'node*'
make[2]: *** [build/Debug/GNU-MacOSX/graph.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

最佳答案

如果我没看错的话应该是这样的:

std::vector<luint> const & getNeighbors() const { return neighbors; }

关于c++ - 常量冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932067/

相关文章:

c++ Cycle随机切断?

c++ - 当类具有 const char* 构造函数时,为什么使用 const char* 变量构造类的未分配临时实例会出错?

c++ - 数组初始值设定项必须是初始值设定项列表或字符串文字

C++11 MinGW 4.9.1 shared_ptr 和 const 静态类字段结果 "Undefined reference"

c++ - 在文本文件中识别编程语言的代码

c# - 为什么 C# 局部变量应该直接赋值,即使它是默认值?

Kubernetes 如何创建一个运行初始化和终止的 sidecar

c++ - 重新分配给 const 引用

PHP OOP - 如何定义带有变量或函数输出的 CONST?

c++ - 我编译的标准库中的 std::endl 在哪里?