Problem with std::list

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 20
Reputation: eliza2044 is an unknown quantity at this point 
Solved Threads: 0
eliza2044 eliza2044 is offline Offline
Newbie Poster

Problem with std::list

 
0
  #1
Jun 23rd, 2009
Hey guys,

I have a problem printing out the contents of a list of objects, with the following code I loop through the list and call the displayInfo() method that it's responsible for printing out the different attributes of the object.
I have tested the displayInfo() method and works fine, however when I'm calling the method when looping through the list I don't gwt the right content.

Please thake a look at the following code and let me know if you see anything wrong.



  1. void displayAircrafts(list <Aircraft>& detectedAircrafts)
  2. {
  3. cout << " DISPLAYING ENTRIES \n";
  4. cout << " ================== \n";
  5. cout << "There are " << detectedAircrafts.size()<< " aircrafts in the list!\n\n";
  6.  
  7. /*loop through the list by using the iterator*/
  8. list <Aircraft>::const_iterator iAircraft;
  9.  
  10. for (iAircraft= detectedAircrafts.begin(); iAircraft != detectedAircrafts.end(); ++iAircraft)
  11. {
  12. Aircraft temp = *iAircraft;
  13. temp.displayInfo();
  14. }
  15. }
And this is how I add objects in the list, note that this is part of the code. My actual code compiles fine:

  1. newAir=addAircraft(&dummy);
  2. aircrafts.push_back(newAir);
  3.  
  4. displayAircrafts(aircrafts);

When I try to display the contents stored in the newAir object by calling the displayInfo method (i.e. newAir.displayInfo()) I get the right result, while the displayAircrafts method gives me something that memory addresses and most of the fields are empty.

Thanks in advance,
Liza
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem with std::list

 
0
  #2
Jun 23rd, 2009
Does your Aircraft class define and implement a copy constructor?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Problem with std::list

 
0
  #3
Jun 23rd, 2009
I can't say what the problem is with what you've shown, but the list makes a copy of the object. Check your copy constructor for the Aircraft class.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: eliza2044 is an unknown quantity at this point 
Solved Threads: 0
eliza2044 eliza2044 is offline Offline
Newbie Poster

Re: Problem with std::list

 
0
  #4
Jun 23rd, 2009
I've been looking to this problem for a while but I'm sure that it is something really stupid but I can't find what it is.

Here's the aircraft.cpp file:

  1. #include "aircraft.h"
  2.  
  3. Aircraft::Aircraft()
  4. {
  5. longitude="";
  6. latitude="";
  7. altitude="";
  8. country="";
  9. flight="";
  10. code="";
  11. earliestTime="";
  12. latestTime="";
  13.  
  14. isOnGround=true;
  15. speed=0.0;
  16. track=0.0;
  17. verticalRate=0.0;
  18. }
  19. Aircraft::Aircraft(string& newLongitude, string& newLatitude, string& newAltitude,string& newCountry, string& newFlight, string& newCode, string& newEarliestTime, string& newLatestTime, bool& newBool, double& newSpeed, double& newTrack, double& newRate)
  20. {
  21. longitude=newLongitude;
  22. latitude=newLatitude;
  23. altitude=newAltitude;
  24. country=newCountry;
  25. flight=newFlight;
  26. code=newCode;
  27. earliestTime=newEarliestTime;
  28. latestTime=newLatestTime;
  29.  
  30. isOnGround= newBool;
  31. speed= newSpeed;
  32. track= newTrack;
  33. verticalRate= newRate;
  34. }
  35.  
  36. Aircraft::Aircraft(string newLongitude, string newLatitude, string newAltitude,
  37. string newCountry, string newFlight, string newCode, string newEarliestTime, string newLatestTime, bool newBool, double newSpeed, double newTrack, double newRate)
  38. {
  39. longitude=newLongitude;
  40. latitude=newLatitude;
  41. altitude=newAltitude;
  42. country=newCountry;
  43. flight=newFlight;
  44. code=newCode;
  45. earliestTime=newEarliestTime;
  46. latestTime=newLatestTime;
  47.  
  48. isOnGround= newBool;
  49. speed= newSpeed;
  50. track= newTrack;
  51. verticalRate= newRate;
  52. }
  53.  
  54. void Aircraft::setAll(string newLongitude, string newLatitude, string newAltitude,
  55. string newCountry, string newFlight, string newCode, string newEarliestTime, string newLatestTime, bool newBool, double newSpeed, double newTrack, double newRate)
  56. {
  57. longitude=newLongitude;
  58. latitude=newLatitude;
  59. altitude=newAltitude;
  60. country=newCountry;
  61. flight=newFlight;
  62. code=newCode;
  63. earliestTime=newEarliestTime;
  64. latestTime=newLatestTime;
  65.  
  66. isOnGround= newBool;
  67. speed= newSpeed;
  68. track= newTrack;
  69. verticalRate= newRate;
  70. }
  71.  
  72. Aircraft::Aircraft(const Aircraft& orig) {
  73. }
  74.  
  75. Aircraft::~Aircraft() {
  76. }
  77.  
  78. //set methods
  79. void Aircraft::setCoords(string newLong, string newLat, string newAlt )
  80. {
  81. longitude= newLong;
  82. latitude= newLat;
  83. altitude= newAlt;
  84. }
  85.  
  86. void Aircraft::setEarliestTime(string newTime)//the time that the aircraft was first detected
  87. {
  88. earliestTime=newTime;
  89. }
  90.  
  91. void Aircraft::setLatestTime(string newTime)
  92. {
  93. latestTime = newTime;
  94. }
  95.  
  96. void Aircraft::setCode(string newCode)//this is the uniques aircraft ID, column 3 in the bst file
  97. {
  98. code=newCode;
  99. }
  100. void Aircraft::setFlight(string newFlight)// column 5
  101. {
  102. flight = newFlight;
  103. }
  104.  
  105. void Aircraft::setCountry(string newCountry)//column 6
  106. {
  107. country=newCountry;
  108. }
  109.  
  110. void Aircraft::setLong(string newLong) //column 11 in the bst file
  111. {
  112. longitude = newLong;
  113. }
  114.  
  115. void Aircraft::setLat(string newLat) // column 10 in the bst file
  116. {
  117. latitude = newLat;
  118. }
  119.  
  120. void Aircraft::setAlt(string newAlt) // column 8 and 9 in the bst file (in feet)
  121. {
  122. altitude= newAlt;
  123. }
  124. void Aircraft::setIsOnGround(bool newBool)
  125. {
  126. isOnGround=newBool;
  127. }
  128.  
  129. void Aircraft::setTrack(double newTrack)
  130. {
  131. track=newTrack;
  132. }
  133.  
  134. void Aircraft::setSpeed(double newSpeed)
  135. {
  136. speed=newSpeed;
  137. }
  138.  
  139. void Aircraft::setVerticalRate(double newRate)
  140. {
  141. verticalRate=newRate;
  142. }
  143.  
  144.  
  145.  
  146.  
  147. //get methods
  148. string Aircraft::getCode()
  149. {
  150. return code;
  151. }
  152.  
  153. string Aircraft::getFlight()
  154. {
  155. return flight;
  156. }
  157.  
  158. string Aircraft::getCountry()
  159. {
  160. return country;
  161. }
  162.  
  163. string Aircraft::getLong()
  164. {
  165. return longitude;
  166. }
  167.  
  168. string Aircraft::getLat()
  169. {
  170. return latitude;
  171. }
  172.  
  173. string Aircraft::getAlt()
  174. {
  175. return altitude;
  176. }
  177.  
  178. string Aircraft::getEarliestTime()
  179. {
  180. return earliestTime;
  181. }
  182.  
  183. string Aircraft::getLatestTime()
  184. {
  185. return latestTime;
  186. }
  187.  
  188. bool Aircraft::getIsOnGround()
  189. {
  190. return isOnGround;
  191. }
  192.  
  193. double Aircraft::getTrack()
  194. {
  195. return track;
  196. }
  197.  
  198. double Aircraft::getSpeed()
  199. {
  200. return speed;
  201. }
  202.  
  203. double Aircraft::getVerticalRate()
  204. {
  205. return verticalRate;
  206. }
  207.  
  208. /* OPERATIONS */
  209. void Aircraft::displayInfo()
  210. {
  211. std::cout << "Displaying Aircraft Information\n";
  212. std::cout << "-------------------------------\n";
  213. std::cout << "Longitude:\t" << longitude << "\n";
  214. std::cout << "Latitude:\t" << latitude << "\n";
  215. std::cout << "Altitude:\t" << altitude << "\n";
  216. std::cout << "Country:\t" << country << "\n";
  217. std::cout << "Callsign:\t" << flight << "\n";
  218. std::cout << "ModeS add:\t" << code << "\n";
  219. std::cout << "Earliest Time:\t" << earliestTime << "\n";
  220. std::cout << "Latest Time:\t" << latestTime << "\n";
  221. std::cout << "Landed:\t\t" << isOnGround << "\n";
  222. std::cout << "Speed:\t\t" << speed << "\n";
  223. std::cout << "Track:\t\t" << track << "\n";
  224. std::cout << "Vertical Rate:\t" << verticalRate << "\n";
  225. std::cout << "\n\n\n";
  226. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Problem with std::list

 
0
  #5
Jun 23rd, 2009
You have a copy constructor, but it doesn't copy anything.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: eliza2044 is an unknown quantity at this point 
Solved Threads: 0
eliza2044 eliza2044 is offline Offline
Newbie Poster

Re: Problem with std::list

 
0
  #6
Jun 23rd, 2009
Guys thanks for your very quick responses.

I have followed a similar approach for a different project and it worked fine..!

Can you tell me how a copy constructor works because I have no idea?

Thanks in advance..!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: eliza2044 is an unknown quantity at this point 
Solved Threads: 0
eliza2044 eliza2044 is offline Offline
Newbie Poster

Re: Problem with std::list

 
0
  #7
Jun 23rd, 2009
Thanks guys I sorted it out, I can't believe that I didn't see what was the problem!!!

Thanks a lot for your help!!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Problem with std::list

 
0
  #8
Jun 23rd, 2009
Thanks guys I sorted it out
That's great! What did you do to fix it?
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: eliza2044 is an unknown quantity at this point 
Solved Threads: 0
eliza2044 eliza2044 is offline Offline
Newbie Poster

Re: Problem with std::list

 
0
  #9
Jun 23rd, 2009
Originally Posted by Tom Gunn View Post
That's great! What did you do to fix it?
Lol changed the copy constructor...
I initially thought that the problem was with the method that was printing out each object's content.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 10
Reputation: tag5 is an unknown quantity at this point 
Solved Threads: 0
tag5 tag5 is offline Offline
Newbie Poster

Re: Problem with std::list

 
-1
  #10
Jun 23rd, 2009
really, i cant say any thing but u must try always to include
using namespace std; as this defines the preprocessor directitive
ok, thankyou.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC