473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Eror passing const references

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
0 1903

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

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
2
by: John Stiles | last post by:
I have written some pretty simple code which works great in Dev Studio and CodeWarrior, but gcc is giving me link errors. Here is what I've been able to distill it down to: namespace Private {...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
12
by: Bob Altman | last post by:
Hi all, I want to call a function that takes a void* argument and pass it the address of a constant. Is there a way to do this without explicitly defining the constant and then passing the...
4
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my compiler give my the error error: passing `const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.