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.

void displayAircrafts(list <Aircraft>& detectedAircrafts)
{
     cout << "            DISPLAYING ENTRIES            \n";
     cout << "            ==================            \n";
     cout << "There are " << detectedAircrafts.size()<< " aircrafts in the list!\n\n";

     /*loop through the list by using the iterator*/
     list <Aircraft>::const_iterator iAircraft;

     for (iAircraft= detectedAircrafts.begin(); iAircraft != detectedAircrafts.end(); ++iAircraft)
     {
        Aircraft temp = *iAircraft;
        temp.displayInfo();
     }
}

And this is how I add objects in the list, note that this is part of the code. My actual code compiles fine:

newAir=addAircraft(&dummy);
aircrafts.push_back(newAir);

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

Does your Aircraft class define and implement a copy constructor?

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.

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:

#include "aircraft.h"

Aircraft::Aircraft()
{
    longitude="";
    latitude="";
    altitude="";
    country="";
    flight="";
    code="";
    earliestTime="";
    latestTime="";

    isOnGround=true;
    speed=0.0;
    track=0.0;
    verticalRate=0.0;
}
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)
{
    longitude=newLongitude;
    latitude=newLatitude;
    altitude=newAltitude;
    country=newCountry;
    flight=newFlight;
    code=newCode;
    earliestTime=newEarliestTime;
    latestTime=newLatestTime;

    isOnGround= newBool;
    speed= newSpeed;
    track= newTrack;
    verticalRate= newRate;
}

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)
{
    longitude=newLongitude;
    latitude=newLatitude;
    altitude=newAltitude;
    country=newCountry;
    flight=newFlight;
    code=newCode;
    earliestTime=newEarliestTime;
    latestTime=newLatestTime;

    isOnGround= newBool;
    speed= newSpeed;
    track= newTrack;
    verticalRate= newRate;
}

void Aircraft::setAll(string newLongitude, string newLatitude, string newAltitude,
        string newCountry, string newFlight, string newCode, string newEarliestTime, string newLatestTime, bool newBool, double newSpeed, double newTrack, double  newRate)
{
    longitude=newLongitude;
    latitude=newLatitude;
    altitude=newAltitude;
    country=newCountry;
    flight=newFlight;
    code=newCode;
    earliestTime=newEarliestTime;
    latestTime=newLatestTime;

    isOnGround= newBool;
    speed= newSpeed;
    track= newTrack;
    verticalRate= newRate;
}

Aircraft::Aircraft(const Aircraft& orig) {
}

Aircraft::~Aircraft() {
}

//set methods
void Aircraft::setCoords(string newLong, string newLat, string newAlt )
{
    longitude= newLong;
    latitude= newLat;
    altitude= newAlt;
}

void Aircraft::setEarliestTime(string newTime)//the time that the aircraft was first detected
{
    earliestTime=newTime;
}

void Aircraft::setLatestTime(string newTime)
{
    latestTime = newTime;
}

void Aircraft::setCode(string newCode)//this is the uniques aircraft ID, column 3 in the bst file
{
    code=newCode;
}
void Aircraft::setFlight(string newFlight)// column 5
{
    flight = newFlight;
}

void Aircraft::setCountry(string newCountry)//column 6
{
    country=newCountry;
}

void Aircraft::setLong(string newLong) //column 11 in the bst file
{
    longitude = newLong;
}

void Aircraft::setLat(string newLat) // column 10 in the bst file
{
    latitude = newLat;
}

void Aircraft::setAlt(string newAlt) // column 8 and 9 in the bst file (in feet)
{
    altitude= newAlt;
}
void Aircraft::setIsOnGround(bool newBool)
{
    isOnGround=newBool;
}

void Aircraft::setTrack(double newTrack)
{
    track=newTrack;
}

void Aircraft::setSpeed(double newSpeed)
{
    speed=newSpeed;
}

void Aircraft::setVerticalRate(double newRate)
{
    verticalRate=newRate;
}




//get methods
string Aircraft::getCode()
{
    return code;
}

string Aircraft::getFlight()
{
    return flight;
}

string Aircraft::getCountry()
{
    return country;
}

string Aircraft::getLong()
{
    return longitude;
}

string Aircraft::getLat() 
{
    return latitude;
}

string Aircraft::getAlt() 
{
    return altitude;
}

string Aircraft::getEarliestTime()
{
    return earliestTime;
}

string Aircraft::getLatestTime() 
{
    return latestTime;
}

bool Aircraft::getIsOnGround()
{
    return isOnGround;
}

double Aircraft::getTrack()
{
    return track;
}

double Aircraft::getSpeed()
{
    return speed;
}

double Aircraft::getVerticalRate()
{
    return verticalRate;
}

/* OPERATIONS */
void Aircraft::displayInfo()
{
    std::cout << "Displaying Aircraft Information\n";
    std::cout << "-------------------------------\n";
    std::cout << "Longitude:\t" << longitude << "\n";
    std::cout << "Latitude:\t" << latitude << "\n";
    std::cout << "Altitude:\t" << altitude << "\n";
    std::cout << "Country:\t" << country << "\n";
    std::cout << "Callsign:\t" << flight << "\n";
    std::cout << "ModeS add:\t" << code << "\n";
    std::cout << "Earliest Time:\t" << earliestTime << "\n";
    std::cout << "Latest Time:\t" << latestTime << "\n";
    std::cout << "Landed:\t\t" << isOnGround << "\n";
    std::cout << "Speed:\t\t" << speed << "\n";
    std::cout << "Track:\t\t" << track << "\n";
    std::cout << "Vertical Rate:\t" << verticalRate << "\n";
    std::cout << "\n\n\n";
}

You have a copy constructor, but it doesn't copy anything.

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..!

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!!! ;)

Thanks guys I sorted it out

That's great! What did you do to fix it?

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.

really, i cant say any thing but u must try always to include
using namespace std; as this defines the preprocessor directitive
ok, thankyou.

commented: Wrong!! -2

really, i cant say any thing but u must try always to include
using namespace std; as this defines the preprocessor directitive
ok, thankyou.

I will keep that in mind thanks!!! :)

I will keep that in mind thanks!!! :)

No, don't do it, what he says is wrong...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.