【问题标题】:"Expression must have class type" Error, trying to call a class function“表达式必须有类类型”错误,试图调用类函数
【发布时间】:2015-10-25 01:05:44
【问题描述】:

我遇到了以下问题:

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

“Surg1”“Pharm1”和“PatAct1”都带有红色下划线,在 Visual Studio 中出现错误“表达式必须具有类类型”。

我搜索了这个错误,但我不太明白答案,因为我还没有找到向量。有人可以帮我理解为什么这些不只是返回函数返回并将其放在左侧的变量中吗?

如果这是一个非常愚蠢的问题,我很抱歉,我整个星期都被难住了!

#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);
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    代码示例存在几个不同的问题。我将一个一个地看一遍,然后给出一个更综合的答案。

    第一个问题是你的类定义。这里的问题是代码的去向与函数的定义。这与 C++ 中的文件组织有很大关系。让我们从你的一门课开始。

      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
        }
    
    };
    

    我现在已经去掉了一些让人分心的东西。这里的主要问题是这个类中的函数的定义方式是这种方式的两倍。它们在“// Declcare 函数”部分定义一次,第二次在你实际给出身体时定义。

    我认为您在这里遇到的困惑在于代码组织。将类声明到一个文件(例如 Pharmacy.h )然后在另一个文件中定义它是非常典型的。所以一个最基本的类定义可能看起来像这样:

    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;
    }
    

    请注意,这发生在课堂之外。这是为了便于代码组织。通常,您会将所有这些移动到一个单独的文件(名为 Pharmacy.cpp),然后链接到该文件。这是标准方法。还有很多其他的。例如,删除“// Declare functions”部分并在声明函数的同时定义函数体同样有效。对于在单个编译单元中使用的小类,这通常是这样做的。

    所有 3 个类都有这个问题,需要纠正。

    这将我们带到您的主要课程。这里存在变量定义和函数调用的问题。从这个开始:

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

    您不需要“类”说明符。如果您要声明变量,则只需:

    Surgery Surg1;
    Pharmacy Pharm1;
    PatientAccount PatAct1;
    

    根据以下用法,这实际上不适用于您的情况。请注意,这是唯一可以调用构造函数的地方。这个很重要。如果你想调用构造函数:

    Surgery( int &t );
    

    此时你会这样做:

    Surgery Surge1( t )
    

    这将我们带到分散在整个代码中的第二个问题。类型说明符通常仅用于定义(是的,有例外 - 但这是一般规则)。我的意思是这里不需要“int &”部分。那些只是告诉定义,当函数被调用时,它期望引用一个 int。调用函数时,通常只传递你感兴趣的变量。

    上面我提到过以这种方式声明你的类对你不起作用。那是因为在下面的几个地方你试图调用构造函数。像

    这样的行
     Surgery Surg1(int &surgeryanswer);
    

    您要做的是创建一个新变量 Surg1 并使用它。你通常会这样做:

     Surgery Surg1( surgeryanswer );
    

    这将创建一个新的 Surg1。不过,这实际上对您不起作用。这涉及范围界定。取以下代码:

    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;
    

    这里的输出将是:

     2 3 4 5 6 7 8 9 10 11
     10
    

    请注意,在循环内部,您定义的 a 优先 - 或“隐藏”您在循环外部定义的 a。

    在您的程序中,您希望变量在循环的多次运行中保持不变。这意味着您不能在循环内声明它们。有两种方法可以解决这个问题。第一种是使用 get 和 set 方法,而不是重新定义类。换句话说,改变这一行: 外科 Surg1(int &surgeryanswer); 到 Surg1.setType( Surgeryanswr );

    您需要为其他类生成附加函数并执行相同操作。

    将所有这些放在一起会产生一个看起来像这样的类:

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

    【讨论】:

    • 非常感谢塞缪尔的帮助,这太棒了!我对此感到非常失落和不知所措,我真的觉得你教会了我一些我没有得到的东西。谢谢!!!!!!
    【解决方案2】:

    变量(和类)声明有问题。您的变量声明了两次:Surg1Pharm1PatAct1。您必须以这种方式声明此变量:

    Surgery Surg1;
    Pharmacy Pharm1;
    PatientAccount PatAct1;
    

    如果你想从另一个构造函数创建这些变量,那么你必须这样做:

    Surg1 = Surgery(surgeryanswer);
    

    安装于:

    Surgery Surg1(int &surgeryanswer);
    

    对于Pharm1PatAct1 相同:

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

    安装于:

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

    【讨论】:

    • 非常感谢您的评论,这对您有很大帮助!
    • @kittyCarli 我很高兴听到它。如果我或 Samuel 的回答对您有帮助,您能接受其中一个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2011-09-26
    相关资源
    最近更新 更多