views:

56

answers:

5

Sorry but this is really confusing me and I know the answer is staring at me in the face and I can't figure it out. Could someone take a look? Its for a Airline Reservation System school project.

This function takes in the Flight Number, capacity, the count of the number of flights, and the vector containing all of the flights as arguments. It will check to make sure the flight does not exist, if it exists an error message will be displayed, if not the flight will be created. The count for the number of flights will also be incremented.

void newFlight( string f, string s, int* n, vector<Flight> *v)
{

    Flight temp;

    //Check to see if flight number exists
    bool alreadyExist = false;
    for (int i=0; i < v->size(); i++)
    {
        if (f.compare((v[i].getNumber()) == 0))
        alreadyExist = true;
    }

    //If it doesn't exist, create it
    if (!alreadyExist)
    {       
        v->push_back (temp);    //Add the new Flight object to the 
                                            //vector of flights
        *n++;           //Increase count
    }

    else
    {
        cout << "A flight numbered " << f << " already exists"; 
        cout << ". Flight not created.\n";
    }   

};

My problem is that when I try to compare the flight number the flights already in the vector with the one I am trying to add. On line 7 I keep getting this message:

error: ‘class std::vector<Flight, std::allocator<Flight> >’ has no member named ‘getNumber’

The vector in question is a vector with the Flight class which has a member named getNumber(). Im passing the vector to the function by reference. So v is a pointer, but I thought the [] would take care of that. Ive also tried using the -> operator instead of the dot operator but it doesn't help. Im at a loss, any help would be appreciated. Im relatively rusty, the summer just finished :D Also I hope this is formatted correctly.

+3  A: 

If v is a pointer to a vector, then the expression v[n] means the nth vector in the array of vectors v. What you want is (*v)[n].

Peter Ruderman
+4  A: 

Argument v is passed to your function as a pointer to vector. If you then use the [] operator, it thinks, just as in C, that v is a pointer to an array of vectors, so v[i] is actually still a vector.

What you should do is this:

(*v)[i].getNumber();
Patrick
Thanks so much!!! I appreciate all of the quick answers. Just one more thing, when I used it this way inline with the compare function the compiler gave me this error:error: no match for ‘operator==’ in ‘Flight::getNumber()() == 0’So is there anyway to write it all inline without overloading the == operator? This is just out of curiosity, I just ended up saving the output of your code to a temporary string variable and then using that in the compare function instead. Thanks again!
Ben
What is the type returned by getNumber? If this is not a native type (like int) you have to define the operator== to tell the compiler how it should compare two non-native types. If you put the implementation of operator== in the header, the compiler will probably inline this for you (if the implementation of operator== is not too big).
Patrick
+2  A: 

My problem is that when I try to compare the flight number the flights already in the vector with the one I am trying to add. On line 7 I keep getting this message:

You should probably switch over to a sorted container like set or map. This'd be taken care of automatically then.

dirkgently
A: 

Looks like a minor bracket issue.

Try changing this:

 if (f.compare((v[i].getNumber()) == 0))

to

 if (f.compare((v[i].getNumber())) == 0)
skimobear
A: 

A few of the other answers have already nailed the problem: v is a pointer, so you have to use a little different syntax to get what you want.

I would suggest that if such a vector must always be passed to this function (in other world, you would never pass NULL), then you should pass by reference instead of by pointer. In that case your function would take vector<Flight> &v which would then allow you to work with the vector without the extra hassle of pointer syntax. v[i].getNumber() would actually work.

TheUndeadFish