943,691 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 792
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 23rd, 2009
0

Problem with std::list

Expand Post »
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.



c++ Syntax (Toggle Plain Text)
  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:

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eliza2044 is offline Offline
22 posts
since May 2009
Jun 23rd, 2009
0

Re: Problem with std::list

Does your Aircraft class define and implement a copy constructor?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 23rd, 2009
0

Re: Problem with std::list

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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 23rd, 2009
0

Re: Problem with std::list

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:

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eliza2044 is offline Offline
22 posts
since May 2009
Jun 23rd, 2009
0

Re: Problem with std::list

You have a copy constructor, but it doesn't copy anything.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 23rd, 2009
0

Re: Problem with std::list

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..!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eliza2044 is offline Offline
22 posts
since May 2009
Jun 23rd, 2009
0

Re: Problem with std::list

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!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eliza2044 is offline Offline
22 posts
since May 2009
Jun 23rd, 2009
0

Re: Problem with std::list

Quote ...
Thanks guys I sorted it out
That's great! What did you do to fix it?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 23rd, 2009
0

Re: Problem with std::list

Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eliza2044 is offline Offline
22 posts
since May 2009
Jun 23rd, 2009
-1

Re: Problem with std::list

really, i cant say any thing but u must try always to include
using namespace std; as this defines the preprocessor directitive
ok, thankyou.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
tag5 is offline Offline
10 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is there a library with DOS system commands?
Next Thread in C++ Forum Timeline: mscache





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC