我有以下课程
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
答案 0 :(得分:2)
如果我看对了它应该是这样的:
std::vector<luint> const & getNeighbors() const { return neighbors; }
答案 1 :(得分:2)
由于您的方法是const
,因此它必须使用const
this
指针,这意味着您无法修改当前实例的任何字段(就像它们都是{ {1}});这也意味着您无法返回对const
的非const
引用,因为您只有neighbors
访问该字段。
要解决此问题,您应该为const
版const
返回const
引用(当客户端具有getNeighbors
引用或指针时使用您的类的实例,以便它不能通过引用修改const
的内容)和该方法的非neighbors
版本的“普通”引用。这称为const
-overloading。
const
<小时/> 对于新错误:您使用点运算符而不是箭头(访问指针引用的对象的成员所需)。
// Non-const overload
std::vector<luint> & getNeighbors() { return neighbors; }
// const overload
std::vector<luint> const & getNeighbors() const { return neighbors; }
答案 2 :(得分:1)
const限定成员函数承诺不修改对象的状态。这是要记住的关键。
现在,如果这样的成员函数允许返回对成员的引用,使得调用者可以修改该成员,则该函数声明保持该对象不可变的承诺。
修复涉及将返回类型作为'const'的引用(std :: vector const)
答案 3 :(得分:1)
我通过显式转换修改了代码并且它可以正常工作
class node {
friend class graph;
friend ostream& operator<< (ostream&, const 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>& );
luint getID() const { return ID; }
protected:
luint ID;
std::vector<luint> neighbors;
};
class graph {
friend ostream& operator<< (ostream&, graph&);
public:
graph() { }
~graph() { }
void addNode(const node& );
void addNode(const node* );
void readFile(const char * );
void clearMap();
protected:
std::map<luint, node*> nodeMap;
};
void graph::addNode(const node& inNode) {
nodeMap[ (const luint)inNode.ID ] = new node( (const luint) inNode.ID );
nodeMap[ (const luint)inNode.ID ]->neighbors.resize( (const luint) inNode.neighbors.size() );
*nodeMap[ (const luint)inNode.ID ] = inNode;
}
答案 4 :(得分:0)
阅读此C++ FAQ entry。如果要向成员变量返回非const引用,则不能将addNeighbours声明为const。