const conflicting
I have the following 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..
right now the error I get is
graph.h: In member function 'std::vector<long unsigned int, std::allocator<long unsigned int> >&am开发者_运维技巧p; 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
on the other hand, if I remove "const" at the declaration of function "getNeighbors()" I get the error
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
Does anyone know how to fix this issue?
Thank you in advance,
Update:
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());
}
compilation error:
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
If i see it right it should be like this:
std::vector<luint> const & getNeighbors() const { return neighbors; }
Since your method is const
, it has to work with a const
this
pointer, which means that you cannot modify any field of the current instance (it's like they all are const
); this also means that you cannot return a non-const
reference to neighbors
, because all you have is const
access to that field.
To fix the problem, you should return a const
reference for the const
version of getNeighbors
(to be used when the client has a const
reference or pointer to an instance of your class, so that it can't modify the content of neighbors
via the reference) and a "normal" reference for the non-const
version of that method. This is known as const
-overloading.
// Non-const overload
std::vector<luint> & getNeighbors() { return neighbors; }
// const overload
std::vector<luint> const & getNeighbors() const { return neighbors; }
For the new error: you are using the dot operator instead of the arrow (required to access members of an object referenced by pointer).
neighbors->push_back(ID);
A const qualified member function promises not to modify the state of the object. That's the key to remember.
Now if such a member function allows to return a reference to a member such that the caller can modify the member, the function violotes the promise of keeping the object immutable.
The fix involves making the return type as a 'reference to const' (std::vector const)
I modified the code by explicit casting and it works
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;
}
Have a read of this C++ FAQ entry. You cannot declare addNeighbours as const if you're returning a non-const reference to a member variable.
精彩评论