简体   繁体   中英

“Expression must have class type” Error, trying to call a class function

I am having trouble with these lines:

 medcost = Pharm1.getCost();
 surgcost = Surg1.getCost();
 PatientAccount PatAct1(double &medcost, double &surgcost, int &daysanswer);
 finaltotal = PatAct1.getCost();

The "Surg1" "Pharm1" and "PatAct1" are all red underlined with the error "Expression must have class type" in Visual Studio.

I did a search for this error, but I didn't quite understand the answers because I haven't gotten to vectors yet. Would someone please help me understand why these aren't just returning the function returns and placing that in the variables to the left?

I'm sorry if this an incredibly stupid question I have just been stumped all week!

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


class Pharmacy
{
private:
    int type;
    double cost;

public:
    // Declare functions
    Pharmacy();
    Pharmacy(int &t);
    double getCost();

    // Actual functions
    Pharmacy::Pharmacy()
    {
        type = 0;
        cost = 0.00;
    }

    Pharmacy::Pharmacy(int &t)
    {
        type = t;

        switch (type)
        {
        case 1: 
            cost = cost + 100.00;
            break;
        case 2:
            cost = cost + 200.00;
            break;
        case 3:
            cost = cost + 300.00;
            break;
        case 4:
            cost = cost + 400.00;
            break;
        case 5:
            cost = cost + 500.00;
            break;
        }
    }

    double Pharmacy::getCost()
    {
        return cost;
    }

};


class Surgery
{
private:
    int type;
    double cost;

public:
    // Declcare functions 
    Surgery();
    Surgery(int &t);
    void setType(int &t);
    double getCost();

    // Actual functions
    Surgery::Surgery()
    {
        type = 0;
        cost = 0.00;
    }

    Surgery::Surgery(int &t)
    {
        type = t;

        switch (type)
        {
        case 1: 
            cost = 100.00;
            break;
        case 2:
            cost = 200.00;
            break;
        case 3:
            cost = 300.00;
            break;
        case 4:
            cost = 400.00;
            break;
        case 5:
            cost = 500.00;
            break;
        }
    }

    void Surgery::setType(int &t)
    {
        type = t;

        switch (type)
        {
        case 1: 
            cost = 100.00;
            break;
        case 2:
            cost = 200.00;
            break;
        case 3:
            cost = 300.00;
            break;
        case 4:
            cost = 400.00;
            break;
        case 5:
            cost = 500.00;
            break;
        }
    }

    double Surgery::getCost()
    {
        return cost;
    }

};

class PatientAccount
{
private:
    int days;
    double medcost;
    double surgcost;
    double total;
    double rate;

public:
    // Declare functions
    PatientAccount();
    PatientAccount(double &c1, double &c2, int &d);
    double getCost();

    // Actual functions
    PatientAccount::PatientAccount()
    {
        days = 0;
        medcost = 0;
        surgcost = 0;
        total = 0;
        rate = 40.00;
    }

    PatientAccount::PatientAccount(double &c1, double &c2, int &d)
    {
        medcost = c1;
        surgcost = c2;
        rate = 40.00;
        days = d;
        total = (days * rate) + medcost + surgcost;
    }

    double PatientAccount::getCost()
    {
        return total;
    }

};


int main()
{
    // Declare variables
    int menuanswer = 0;
    int surgeryanswer = 0;
    int medicationanswer = 0;
    int daysanswer = 0;
    double finaltotal = 0;
    bool loop = false;
    bool boolsurg = false;
    bool boolmed = false;
    bool booldays = false;
    double medcost = 0;
    double surgcost = 0;

    // Declare class variables
    class Surgery Surg1;
    class Pharmacy Pharm1;
    class PatientAccount PatAct1;

    // Menu loop
    do 
    {
        cout << "Welcome to the menu, what would you like to do?" << endl;
        cout << "Type 1 to enter surgery type" << endl;
        cout << "Type 2 to enter medication type" << endl;
        cout << "Type 3 to enter number of days stayed" << endl;
        cout << "Type 4 to check out and see all costs" << endl;
        cin >> menuanswer;

        switch (menuanswer)
        { 
            case 1:
                cout << "What kind of surgery did you have?" << endl;
                cout << "Press 1 for foot" << endl;
                cout << "Press 2 for brain" << endl;
                cout << "Press 3 for leg" << endl;
                cout << "Press 4 for knee" << endl;
                cout << "Press 5 for hand" << endl;
                cin >> surgeryanswer;
                Surgery Surg1(int &surgeryanswer);
                boolsurg = true;
                loop = false;
                break;
            case 2:
                cout << "What kind of medication did you use?" << endl;
                cout << "Press 1 for pain killers" << endl;
                cout << "Press 2 for heachach pills" << endl;
                cout << "Press 3 for childrens medication" << endl;
                cout << "Press 4 for advil" << endl;
                cout << "Press 5 for tylenol" << endl;
                cin >> medicationanswer;
                Pharmacy Pharm1(int &medicationanswer);
                boolmed = true;
                loop = false;
                break;
            case 3:
                cout << "How many days did you stay at the hospital?" << endl;
                cin >> daysanswer;
                booldays = true;
                loop = false;
                break;
            case 4:
                if ((booldays == true) && (boolmed == true) && (boolsurg == true))
                {
                    cout << "It looks like you're ready to check out" << endl;
                    medcost = Pharm1.getCost();
                    surgcost = Surg1.getCost();
                    PatientAccount PatAct1(double &medcost, double &surgcost, int &daysanswer);
                    finaltotal = PatAct1.getCost();
                    cout << "Your total cost is $" << endl;
                    loop = true;
                }
                else
                {
                    cout << "ERROR - You must give all required information - surgery type, medications, and days" << endl;
                    loop = false;
                }
        }
    } while (loop == false);
}

There are a couple of different issues with the code sample. I will go through them one at a time and then give a more put together answer.

The first issue is in your class definitions. The problem here is where code goes vs. where functions are defined. This has a lot to do with file organization in C++. Let us start with one of your classes.

  class Pharmacy
{
private:
    int type;
    double cost;

public:
    // Declare functions
    Pharmacy();
    Pharmacy(int &t);
    double getCost();

    // Actual functions
    Pharmacy::Pharmacy()
    {
        // do stuff
    }

    Pharmacy::Pharmacy(int &t)
    {
        // do stuff
    }

    double Pharmacy::getCost()
    {
        // do stuff
    }

};

I've stripped some of the stuff out right now that is distracting. The main problem here is that the functions in this class are defined twice the way this is written. They are defined once in the section that says "// Declcare functions" and a second time when you actually give the body.

I think the confusion you are having here is with code organization. It is very typical to declare class into one file (say Pharmacy.h ) and then to define it in another. So a bare minimum class definition might look something like this:

class Pharmacy
{
private:
    int type;
    double cost;

public:
    // Declare functions
    Pharmacy();
    Pharmacy(int &t);
    double getCost();
}

Notice that there is no code involved. Everything there is a declaration. The compiler will then look somewhere else for the actual code for the functions. They can be defined like this:

// Actual functions
Pharmacy::Pharmacy()
{
    type = 0;
    cost = 0.00;
}

Pharmacy::Pharmacy( int &t )
{
    type = t;

    switch ( type )
    {
    case 1:
        cost = cost + 100.00;
        break;
    case 2:
        cost = cost + 200.00;
        break;
    case 3:
        cost = cost + 300.00;
        break;
    case 4:
        cost = cost + 400.00;
        break;
    case 5:
        cost = cost + 500.00;
        break;
    }
}

double Pharmacy::getCost()
{
    return cost;
}

Notice that this happens outside of the class. This is to facilitate code organization. Normally, you would move all of these to a separate file (named Pharmacy.cpp) and then link against that file. This is the standard method. There are a ton of others. For instance, it would be equally valid to delete the "// Declare functions" section and define the function body at the same time you declare the function. For small classes used in a single compilation unit this is frequently done.

All 3 classes have that problem and need to be corrected.

That brings us to your main class. Here there are problems with variable definitions and function calls. Start with this:

// Declare class variables
class Surgery Surg1;
class Pharmacy Pharm1;
class PatientAccount PatAct1;

You don't need the "class" specifier. If you were declaring the variables, you would just do:

Surgery Surg1;
Pharmacy Pharm1;
PatientAccount PatAct1;

This won't actually work in your case based on the usage below. Note that this is the only place that you can call the constructor. This is important. If you wanted to call the constructor:

Surgery( int &t );

You would do so at this point like:

Surgery Surge1( t )

This brings us to the second issue that is scattered throughout the code. Type specifiers are generally only used in definitions (yes, there are exceptions - but it is the general rule). By that I mean that the "int &" portion is not necessary here. Those just tell the definition that when the function is called it is expecting a reference to an int. When you call the function, you usually just pass the variable you are interested in.

Above I mentioned that declaring your classes this way won't work for you. That is because at several places below you are attempting to call the constructor. In lines like

 Surgery Surg1(int &surgeryanswer);

What you want to do is create a new variable Surg1 and use it. You would generally do this like:

 Surgery Surg1( surgeryanswer );

That will create a new Surg1. That won't actually work for you though. This deals with scoping. Take the following code:

int a = 0;
for( int ii=1; ii<=10; ii++ )
{
   int a = 1;
   std::cout << ii+a << " ";
}
std::cout << std:endl << 10+a << std:endl;

The output here is going to be:

 2 3 4 5 6 7 8 9 10 11
 10

Notice that inside the loop, the a that you defined takes precident - or "hides" the a you defined outside the loop.

In your program, you want your variables to persist through multiple runs of the loop. That means you can't declare them inside the loop. There are two ways to deal with this. The first is to use get and set methods instead of redefining the class. In other words change the line: Surgery Surg1(int &surgeryanswer); to Surg1.setType( surgeryanswr );

You need to generate additional functions for the other classes and do the same.

Putting all of that together results in a class that looks something like this:

class Pharmacy
{
private:
    int type;
    double cost;

public:
    Pharmacy();
    Pharmacy( const int &t );
    double getCost();
    void setType( const int& i );
};

// Actual functions
Pharmacy::Pharmacy()
{
    type = 0;
    cost = 0.00;
}

Pharmacy::Pharmacy( const int &t )
{
    type = t;

    switch ( type )
    {
    case 1:
        cost = cost + 100.00;
        break;
    case 2:
        cost = cost + 200.00;
        break;
    case 3:
        cost = cost + 300.00;
        break;
    case 4:
        cost = cost + 400.00;
        break;
    case 5:
        cost = cost + 500.00;
        break;
    }
}

double Pharmacy::getCost()
{
    return cost;
}

void Pharmacy::setType( const int& i ) {
    type = i;
}

class Surgery
{
private:
    int type;
    double cost;

public:
    // Declare functions 
    Surgery();
    Surgery( int &t );
    void setType( int &t );
    double getCost();

};

// Actual functions
Surgery::Surgery()
{
    type = 0;
    cost = 0.00;
}

Surgery::Surgery( int &t )
{
    type = t;

    switch ( type )
    {
    case 1:
        cost = 100.00;
        break;
    case 2:
        cost = 200.00;
        break;
    case 3:
        cost = 300.00;
        break;
    case 4:
        cost = 400.00;
        break;
    case 5:
        cost = 500.00;
        break;
    }
}

void Surgery::setType( int &t )
{
    type = t;

    switch ( type )
    {
    case 1:
        cost = 100.00;
        break;
    case 2:
        cost = 200.00;
        break;
    case 3:
        cost = 300.00;
        break;
    case 4:
        cost = 400.00;
        break;
    case 5:
        cost = 500.00;
        break;
    }
}

double Surgery::getCost()
{
    return cost;
}

class PatientAccount
{
private:
    int days;
    double medcost;
    double surgcost;
    double total;
    double rate;

public:
    // Declare functions
    PatientAccount();
    PatientAccount( double &c1, double &c2, int &d );
    double getCost();
    void setValues( double& c1, double &c2, int &d );
};

// Actual functions
PatientAccount::PatientAccount()
{
    days = 0;
    medcost = 0;
    surgcost = 0;
    total = 0;
    rate = 40.00;
}

PatientAccount::PatientAccount( double &c1, double &c2, int &d )
{
    medcost = c1;
    surgcost = c2;
    rate = 40.00;
    days = d;
    total = ( days * rate ) + medcost + surgcost;
}

void PatientAccount::setValues( double &c1, double &c2, int &d )
{
    medcost = c1;
    surgcost = c2;
    rate = 40.00;
    days = d;
    total = ( days * rate ) + medcost + surgcost;
}

double PatientAccount::getCost()
{
    return total;
}


int main( )
{
    // Declare variables
    int menuanswer = 0;
    int surgeryanswer = 0;
    int medicationanswer = 0;
    int daysanswer = 0;
    double finaltotal = 0;
    bool loop = false;
    bool boolsurg = false;
    bool boolmed = false;
    bool booldays = false;
    double medcost = 0;
    double surgcost = 0;

    // Declare class variables
    Surgery Surg1;
    Pharmacy Pharm1;
    PatientAccount PatAct1;

    // Menu loop
    do
    {
        cout << "Welcome to the menu, what would you like to do?" << endl;
        cout << "Type 1 to enter surgery type" << endl;
        cout << "Type 2 to enter medication type" << endl;
        cout << "Type 3 to enter number of days stayed" << endl;
        cout << "Type 4 to check out and see all costs" << endl;
        cin >> menuanswer;

        switch ( menuanswer )
        {
        case 1:
            cout << "What kind of surgery did you have?" << endl;
            cout << "Press 1 for foot" << endl;
            cout << "Press 2 for brain" << endl;
            cout << "Press 3 for leg" << endl;
            cout << "Press 4 for knee" << endl;
            cout << "Press 5 for hand" << endl;
            cin >> surgeryanswer;
            Surg1.setType( surgeryanswer );
            boolsurg = true;
            loop = false;
            break;
        case 2:
            cout << "What kind of medication did you use?" << endl;
            cout << "Press 1 for pain killers" << endl;
            cout << "Press 2 for heachach pills" << endl;
            cout << "Press 3 for childrens medication" << endl;
            cout << "Press 4 for advil" << endl;
            cout << "Press 5 for tylenol" << endl;
            cin >> medicationanswer;
            Pharm1.setType( medicationanswer );
            boolmed = true;
            loop = false;
            break;
        case 3:
            cout << "How many days did you stay at the hospital?" << endl;
            cin >> daysanswer;
            booldays = true;
            loop = false;
            break;
        case 4:
            if ( ( booldays == true ) && ( boolmed == true ) && ( boolsurg == true ) )
            {
                cout << "It looks like you're ready to check out" << endl;
                medcost = Pharm1.getCost();
                surgcost = Surg1.getCost();
                PatAct1.setValues( medcost, surgcost, daysanswer );
                finaltotal = PatAct1.getCost();
                cout << "Your total cost is $" << endl;
                loop = true;
            }
            else
            {
                cout << "ERROR - You must give all required information - surgery type, medications, and days" << endl;
                loop = false;
            }
        }
    } while ( loop == false );

}

You have some problem with variable (and classes) declaration. Your variables declared twice: Surg1 , Pharm1 and PatAct1 . You must declare this variables in this way:

Surgery Surg1;
Pharmacy Pharm1;
PatientAccount PatAct1;

If you want create these varibales from another constructor then you must do this:

Surg1 = Surgery(surgeryanswer);

Insted of:

Surgery Surg1(int &surgeryanswer);

For Pharm1 and PatAct1 same:

Pharm1 = Pharmacy(medicationanswer);
PatAct1 = PatientAccount(medcost, surgcost, daysanswer);

Insted of:

Pharmacy Pharm1(int &medicationanswer);
PatientAccount PatAct1(double &medcost, double &surgcost, int &daysanswer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM