"表达式必须具有类型类型"错误,尝试调用类函数

时间:2015-10-25 01:05:45

标签: c++

我遇到这些问题:

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

2 个答案:

答案 0 :(得分:3)

代码示例存在两个不同的问题。我将逐个浏览它们,然后再给出更多的答案。

第一个问题出在您的班级定义中。这里的问题是代码与定义函数的位置。这与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;

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

Surgery Surg1;
Pharmacy Pharm1;
PatientAccount PatAct1;

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

Surgery( int &t );

你会这样做:

Surgery Surge1( t )

这将我们带到分散在整个代码中的第二个问题。类型说明符通常仅用于定义(是的,有例外 - 但它是一般规则)。我的意思是“int&amp;”这里没有必要的部分。那些只是告诉定义,当调用函数时,它期望引用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&amp; 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 );

}

答案 1 :(得分:0)

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

Surgery Surg1;
Pharmacy Pharm1;
PatientAccount PatAct1;

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

Surg1 = Surgery(surgeryanswer);

Insted of:

Surgery Surg1(int &surgeryanswer);

对于Pharm1PatAct1相同:

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

Insted of:

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