By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,508 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,508 IT Pros & Developers. It's quick & easy.

Eror passing const references

P: 2
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from.

so here is the error I'm getting:
Expand|Select|Wrap|Line Numbers
  1. TwoDMap.cpp: In member function 'void TwoDMap::setArc(const Line&, uint32_t)':
  2. TwoDMap.cpp:17: error: passing 'const NodeInfo' as 'this' argument of 'void NodeInfo::modifyArc(const Point&, uint32_t)' discards qualifiers
  3. TwoDMap.cpp: In member function 'uint32_t TwoDMap::getArcCost(const Line&) const':
  4. TwoDMap.cpp:35: error: passing 'const NodeInfo' as 'this' argument of 'Internal::hashtable_iterator<std::pair<const Point, unsigned int>, false, false> NodeInfo::getArc(const Point&)' discards qualifiers
here is my TwoDMap.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "TwoDMap.h"
  2.  
  3. TwoDMap::TwoDMap()
  4. {
  5. }
  6.  
  7. void TwoDMap::addNode(const NodeInfo& newinfo)
  8. {
  9.         nodes.insert(newinfo);
  10. }
  11.  
  12. void TwoDMap::setArc(const Line& arc, const uint32_t newcost)
  13. {
  14.         NodeInfo keynode(arc.from);
  15.         NodeContainer::iterator origin(nodes.find(keynode));
  16.         // if origin = nodes.end() throw an error
  17.         origin->modifyArc(arc.to, newcost);
  18. }
  19.  
  20. //return a pointer to a constant?
  21. const NodeInfo& TwoDMap::getNodeInfo(const Point& node)const
  22. {
  23.         NodeInfo keynode(node);
  24.         NodeContainer::iterator origin(nodes.find(keynode));
  25.  
  26.         return *origin;
  27. }
  28.  
  29. //return a pinter to a constant?
  30. uint32_t TwoDMap::getArcCost(const Line& arc)const
  31. {
  32.         NodeInfo keynode(arc.from);
  33.         NodeContainer::iterator origin(nodes.find(keynode));
  34.         // if origin = nodes.end() throw an error
  35.         ArcContainer::iterator destination(origin->getArc(arc.to));
  36.         return destination->second;
  37.  
  38. }
and here is NodeInfo.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "NodeInfo.h"
  2.  
  3. NodeInfo::NodeInfo()
  4. {
  5.         backpointer.x=0;
  6.         backpointer.y=0;
  7.         name.x=0;
  8.         name.y=0;
  9.         hvalue = 4294967295U;
  10.         tag = NEW;
  11. }
  12.  
  13. NodeInfo::NodeInfo(const Point& newname)
  14. {
  15.         name = newname;
  16.         backpointer.x=0;
  17.         backpointer.y=0;
  18.         hvalue = 4294967295U;
  19.         tag = NEW;
  20. }
  21.  
  22. void NodeInfo::setBP(const Point& newbp)
  23. {
  24.         backpointer = newbp;
  25. }
  26.  
  27. void NodeInfo::setHV(const uint32_t nhv)
  28. {
  29.         hvalue = nhv;
  30. }
  31.  
  32. Point NodeInfo::getBP(void) const
  33. {
  34.         return backpointer;
  35. }
  36.  
  37. state NodeInfo::getState(void) const
  38. {
  39.         return tag;
  40. }
  41.  
  42.  
  43. uint32_t NodeInfo::getHV(void) const
  44. {
  45.         return hvalue;
  46. }
  47.  
  48. Point NodeInfo::getName(void) const
  49. {
  50.         return name;
  51. }
  52.  
  53. void NodeInfo::open(void)
  54. {
  55.         tag = OPEN;
  56. }
  57.  
  58. void NodeInfo::close(void)
  59. {
  60.         tag = CLOSSED;
  61. }
  62.  
  63. void NodeInfo::openwith(const Point& newbp)
  64. {
  65.         tag = OPEN;
  66.         backpointer = newbp;
  67. }
  68.  
  69. void NodeInfo::closewith(const Point& newbp)
  70. {
  71.         tag = CLOSSED;
  72.         backpointer = newbp;
  73. }
  74.  
  75. void NodeInfo::modifyArc(const Point& toarc, const uint32_t arcCost)
  76. {
  77.         arcs[toarc]=arcCost;
  78. }
  79.  
  80. ArcContainer::iterator NodeInfo::getArc(const Point& toarc)
  81. {
  82.         return arcs.find(toarc);
  83. }
  84.  
  85. ArcContainer::iterator NodeInfo::inspectB()
  86. {
  87.         return arcs.begin();
  88. }
  89.  
  90. ArcContainer::iterator NodeInfo::inspectE()
  91. {
  92.         return arcs.end();
  93. }
  94.  
  95. bool NodeInfo::isopen()
  96. {
  97.         return (tag == OPEN);
  98. }
  99.  
  100. bool NodeInfo::isclosed()
  101. {
  102.         return (tag == CLOSSED);
  103. }
  104.  
  105. bool NodeInfo::isnew()
  106. {
  107.         return (tag == NEW);
  108. }
  109.  
  110. //end of NodeInfo but to define the structs
  111. bool NodeInfoEq::operator()(const NodeInfo& lhs, const NodeInfo& rhs) const
  112. {
  113.         return (lhs.getName() == rhs.getName());
  114. }
  115.  
  116. bool operator>(const NodeInfo& lhs, const NodeInfo& rhs)
  117. {
  118.         return (lhs.hvalue > rhs.hvalue);
  119. }
  120.  
  121. bool operator<(const NodeInfo& lhs, const NodeInfo& rhs)
  122.  
  123. {
  124.         return (lhs.hvalue < rhs.hvalue);
  125. }
As you can see I'm using the tr1 library, consequently I have the version of gcc that does not have defalut constructors for the unordered set Iterators (gcc 4.0.3), that's why I used the copy constructor. I also had to add this to the nodeinfo.h to use store it in a unordered hash:
Expand|Select|Wrap|Line Numbers
  1. struct NodeInfoHash
  2. {
  3.     size_t operator()(NodeInfo key) const
  4.     {
  5.         std::tr1::hash<Point> phash;
  6.         return (phash(key.getName()) );
  7.     }
  8. };
  9.  
  10. struct NodeInfoEq
  11. {
  12.     bool operator()(const NodeInfo&, const NodeInfo&) const;
  13. };
and then declare this in TwoDMap.h:
Expand|Select|Wrap|Line Numbers
  1. typedef std::tr1::unordered_set<NodeInfo, NodeInfoHash, NodeInfoEq> NodeContainer;
Here is some supporting structures that I made:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _MAPTYPES_H_
  2. #define _MAPTYPES_H_
  3.  
  4. #include <stdint.h>
  5. #include <tr1/functional>
  6. #include <tr1/unordered_map>
  7.  
  8. struct Point
  9. {
  10.         int x;
  11.         int y;
  12.         Point(int nx=0, int ny=0): x(nx), y(ny){}
  13.         Point(const Point& np): x(np.x), y(np.y){}
  14.         friend int operator==(const Point& left, const Point& right)
  15.                 {return left.x==right.x && left.y==right.y;}
  16. };
  17.  
  18. struct Line
  19. {
  20.         Point from;
  21.         Point to;
  22. };
  23.  
  24. namespace std {
  25. namespace tr1 {
  26.  
  27.         template <>
  28.         struct hash<Point>
  29.         {
  30.                 std::size_t operator()(const Point& p) const
  31.                 {
  32.                         hash<int> mkhash;
  33.                         return ( mkhash(p.x) + mkhash(p.y) );
  34.                 }
  35.         };
  36.  
  37.         template <>
  38.         struct hash<Line>
  39.         {
  40.                 std::size_t operator()(const Line& l) const
  41.                 {
  42.                         hash<int> mkhash;
  43.                         return ( mkhash(l.to.x) + mkhash(l.to.y) + mkhash(l.from.x) + mkhash(l.from.y) );
  44.                 }
  45.         };
  46.  
  47. }}//std and tr1
  48.  
  49. typedef std::tr1::unordered_map<Point, uint32_t> ArcContainer;
  50.  
  51. #endif
I'm trying to relearn C++ so i'm not sure how to fix this error or exactly what it means. Also I realize the forum is for problems with the language but is my design reasonable or should i have used the unordered map? Bottom line: Am I on the right track to a good object oriented design or am I doomed to fail? ...inquiring minds want to know.
Mar 31 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.