c++ - “Expression must have class type”错误,尝试调用类函数

标签 c++

我在以下方面遇到了麻烦:

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

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

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

如果这是一个非常愚蠢的问题,我整个星期都感到难过,对不起!
#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++中的文件组织有很大关系。让我们从您的一门课开始。

  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函数”一节中定义一次,而在您实际给出 body 时第二次定义。

我认为您在这里的困惑在于代码组织。将类声明到一个文件中(例如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),然后针对该文件进行链接。这是标准方法。还有很多其他的。例如,在声明函数的同时删除“//声明函数”部分并​​定义函数主体同样有效。对于单个编译单元中使用的小类,通常会这样做。

所有3个类都有此问题,需要更正。

那把我们带到了你的主要类(class)。这里有变量定义和函数调用的问题。从此开始:
// Declare class variables
class Surgery Surg1;
class Pharmacy Pharm1;
class PatientAccount PatAct1;

您不需要“类”说明符。如果要声明变量,则只需执行以下操作:
Surgery Surg1;
Pharmacy Pharm1;
PatientAccount PatAct1;

根据下面的用法,这实际上不适用于您的情况。请注意,这是您可以调用构造函数的唯一的位置。这个很重要。如果要调用构造函数:
Surgery( int &t );

您现在可以这样做:
Surgery Surge1( t )

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

上面我提到过,以这种方式声明您的类(class)对您不起作用。那是因为您在下面的几个地方都试图调用构造函数。像
 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(urgicalanswr);

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

将所有这些放在一起会得到一个看起来像这样的类:
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 );

}

关于c++ - “Expression must have class type”错误,尝试调用类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325067/

相关文章:

C++ 在编译时创建 BSTR/在编译时将长度插入字符串?

c++ - 无法使用 findcontours 和 opencv 找到所有轮廓

c++ - 初始化程序中的多个突变是否列出了未定义的行为?

c++ - 为什么不能显示指针的地址?

c++ - 如何将 texelFetch 与 GL_TEXTURE_2D 一起使用?

c++ - 当内存分配给函数时(定义或调用)

c++ - 协程参数的生命周期是多少

c++ - 外部 "C"或不外部 "C"[g++ vs cl]

c++ - 为什么这段使用 printf 和 cout 的代码没有预期的输出?

c++ - gluLookAt 是如何工作的?