I'm having some real problem with a list of objects. My objects in this case are called Aircraft & I've given them these features:

int flightNo;

void setFlightNo(int i)
{
    flightNo = i; 
}

int getFlightNo()
{
   return flightNo;
}

Ok, all good - until I put them in a list - list<Aircraft> landed.
I've tried several implementations including
1) Have the list as a variable of the main.cpp file
2) Write a wrapper class for the list
2i) Have main generate new aircraft & pass by value to wrapper class to add to list
2ii) Have wrapper class create new aircraft itself & add to list

No success so far. Before I put the aircraft in the list

cout << aircraft.getFlightNo()

always shows the correct value. After pushing, aircraft into landed

cout << landed.front.getFlightNo()

, or a wrapper class serving an Aircraft object using the getFlightNo method gives me either 0 or a very large number usually starting with 3.
I don't know if this is relevant but the method for generating the aircraft & adding it to the list is being called from a pthread.
Any assistance would be greatly appreciated!

Edited 7 Years Ago by skirmish: n/a

front is a function, so you need ()

#include <iostream>
#include <list>
using namespace std;

class Aircraft
{
 public:
    Aircraft(int i = 0) { flightNo = i; }
    int getFlightNo() { return flightNo; }
    void  setFlightNo(int i) { flightNo = i; }
 protected:
    int flightNo;
};

int main()
{
    list<Aircraft> landed;
    landed.push_back(Aircraft(1));
    landed.push_back(Aircraft(2));
    cout << landed.front().getFlightNo() << "\n";
    return 0;
}

Edited 7 Years Ago by Ancient Dragon: n/a

Hey Ancient Dragon, thanks for the reply. My bad, I did include that in my project but forgot to include it in the post.
The project compiles & runs but no matter what I try pulling the Aircraft back out of the list (also tried queue with the same result) leaves them with apparently uninitialized variables.

Edited 7 Years Ago by skirmish: n/a

Sure thing. It's a bit stripped down because it's an assignment, can't publish everything. Hopefully this is enough to show where I'm having trouble.

Airport.cpp

#include "airport.h"
#include "aircraft.h"

Airport::Airport() {
}

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

Airport::~Airport() {
}

void Airport::land(Aircraft a)
{
    cout << "Land method, flight no: " << a.getFlightNo() << endl;
    landed.push(a);
}

Aircraft Airport::serve()
{
    Aircraft temp = landed.front();
    return temp;
}

int Airport::amountOfAircraft()
{
    return landed.size();
}

main.cpp

#include <stdlib.h>
#include <list>
#include "aircraft.h"
#include "suv.h"
#include <stdio.h>
#include <cstdlib>
#include <list.h>
#include <unistd.h>
#include <pthread.h>
#include <queue.h>
#include "airport.h"

using namespace std;
int runningTotal, landingDelay, unloadFromAircraftDelay, unloadFromSUVDelay;
queue<Aircraft> landed(), unloaded;
Airport melbourne;
SUV su;
void *land(void *ptr);
void *unloadFromAircraft(void *ptr);
void *unloadFromSUV(void *ptr);
void *takeOff(void *ptr);
void *incrementClock(void *ptr);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;


int main(int argc, char** argv)
{
    runningTotal=0;

    //Set Delay Variables
    landingDelay = 10;
    unloadFromAircraftDelay = 2;
    unloadFromSUVDelay = 1;


    int thread_ret1, thread_ret2, thread_ret3;
    pthread_t thread1, thread2, thread3;
    SUV su;
    
    while(true)
    {
        //Land Aircraft Thread
        if( (thread_ret1=pthread_create(&thread1, NULL, &land, NULL) ) )
        {
            cout << "Thread creation failed" << thread_ret1 << endl;
        }
        
        //Unload cargo from aircraft thread
        if( (thread_ret2=pthread_create(&thread2, NULL, &unloadFromAircraft, NULL) ) )
        {
            cout << "Thread creation failed" << thread_ret2 << endl;
        }
    }
    
    return (EXIT_SUCCESS);
}

void *land(void *ptr)
{
     pthread_mutex_lock(&mutex1);
     int timeToSleep = rand() % landingDelay + landingDelay;
     runningTotal++;
     totalAtAirport++;
     Aircraft a;
     a.setFlightNo(runningTotal);
     melbourne.land(a);
     sleep(timeToSleep);
     pthread_mutex_unlock(&mutex1);
}

void *unloadFromAircraft(void *ptr)
{
    if(melbourne.amountOfAircraft()>0)
    {
        pthread_mutex_lock(&mutex2);
        pthread_mutex_unlock(&mutex2);
    }
}

PS. Yeah, changed from list to queue but no noticeable difference. Someone recommended passing as pointers but I don't see why this would help, also the original object would be destroyed as soon as the method ends leaving a reference to an empty area of memory so I'm passing by value. Perhaps this where the objects lose their information?

Edited 7 Years Ago by skirmish: n/a

This article has been dead for over six months. Start a new discussion instead.