0

I'm having a problem with the code below. Can anyone help me?

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



class Aircraft
{
public:
        Aircraft() : MPrice(0), LPrice(0), BPrice(0), PPrice(0), TotalPrice(0) , Stars(0) {cout<<"aircraft constructor"<<endl;}
        virtual ~Aircraft(){cout<<"aircraft destructor"<<endl;}

protected:
    string Missile;
    string Laser;
    string Bomb;
    string Propulsion;
    int MPrice, LPrice, BPrice, PPrice, TotalPrice;
    int Stars;
};

class Jet : public Aircraft
{
public:
     string GetMissile() {return Missile;}
     int MissilePrice() {return MPrice;}

     string GetLaser() {return Laser;}
     int LaserPrice() {return LPrice;}

     string GetBomb() {return Bomb;}
     int BombPrice() {return BPrice;}

     string GetPropulsion() {return Propulsion;}
     int PropulsionPrice() {return PPrice;}

     void SetMissile(int MissileChoice)
     {
         switch(MissileChoice)
         {

         case 0:  Missile = "Air-to-air missile"; 
                    MPrice = 300;
                    break;
         case 1:  Missile = "Air-to-surface missile"; 
                    MPrice = 600;
                    break;
         }
     }
     void SetLaser(int LaserChoice)
     {
         switch(LaserChoice)
         {
         case 0:  Laser = "Hydrogen Fluoride Laser"; 
                    LPrice = 100;
                    break;
         case 1:  Laser = "Deuterium Fluoride Laser"; 
                    LPrice = 225;
                    break;
         }
     }
    void SetBomb(int BombChoice)
     {
         switch(BombChoice)
         {
         case 0:    Bomb = "Cluster Bomb"; 
                    BPrice = 600;
                    break;
         case 1:  Bomb = "Concrete Bomb"; 
                    BPrice = 700;
                    break;
         }
     }
    void SetPropulsion(int PropulsionChoice)
     {
         //Jet.Propulsion
         switch(PropulsionChoice)
         {
         case 0:  Propulsion = "Solid-fuel Propulsion"; 
                    PPrice = 750;
                    break;
         case 1:  Propulsion = "Liquid Rocket Propulsion"; 
                    PPrice = 849;
                    break;
         }
     }

    int GetTotalPrice() {TotalPrice = MPrice+LPrice+BPrice+PPrice;
                            return TotalPrice;}

    int GetStars(int (Jet::*pGTPrice)() )
    {

        int Price = pGTPrice();
    cout<<GetTotalPrice()<<"-PRICE!"<<endl;
    for (int i=1; i<13; i++)
        {
        if ( Price>=(1200+(100*i)) && Price< (1300+(100*i)) )
            {
            Stars=i;
            cout<<i<<"-STARS!"<<endl;
            //return Stars;
            break;
            }
        }
    return Stars;
    }

};// end class jet: public aircraft


int main()
{
    Jet You;
    int MissileChoice, LaserChoice, BombChoice, PropulsionChoice;
    cout<<"choose the missile, laser, bomb, propulsion of the jet"<<endl;
    cin>>MissileChoice;
    cin>>LaserChoice;
    cin>>BombChoice;
    cin>>PropulsionChoice;
    You.SetMissile(MissileChoice);
    You.SetLaser(LaserChoice);
    You.SetBomb(BombChoice);
    You.SetPropulsion(PropulsionChoice);
    cout<<"you chose "<<endl;
    cout<<You.GetMissile()<<endl;
    cout<<You.GetLaser()<<endl;
    cout<<You.GetBomb()<<endl;
    cout<<You.GetPropulsion()<<endl;

    cout<<endl<<"total price is "<<You.GetTotalPrice()<<endl;
    Jet* ptr=0;
    ptr=new Jet;
    int (Jet::*pPrice)()  = Jet::GetTotalPrice;
    cout<<"You have produced a "<<ptr->GetStars(pPrice)<<"-starred JET!";
    delete ptr;

    cin.ignore();
    cin.ignore();


return 0;
}

I'm getting an error with

int Price = pGTPrice();

inside the GetStars function

3
Contributors
2
Replies
6
Views
4 Years
Discussion Span
Last Post by jacob.lemelincarrier
0

The parameter pGTPrice is a pointer-to-member-function. And to call the member function it points too, you need to supply an object on which to call it. Within the "GetStars" function, you should use the "this" pointer to address the object you want to call the member function on, you can do that as follows:

int Price = (this->*pGTPrice)();

That should do it.

Edited by mike_2000_17: addition

0

Seeing as I couldn't find any errors in your code, I believe all you forgot to do is dereference the pointer to access the function.

int Price = (*pGTPrice)();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.