0

我正在尝试运行我编写的这个 c++ 程序。我在 Visual C++ 中遇到以下错误:

1>c:\users\server\desktop\problem7\problem7\main.cpp(26) : 错误 C3867: 'Payment::getamount': 函数调用缺少参数列表;使用 '&Payment::getamount' 创建指向成员的指针

1>c:\users\server\desktop\problem7\problem7\main.cpp(74) : 错误 C3867: 'cashPayment::paymentDetails': 函数调用缺少参数列表;使用 '&cashPayment::paymentDetails' 创建指向成员的指针

1>c:\users\server\desktop\problem7\problem7\main.cpp(75) : 错误 C3867: 'CreditCardPayment::paymentDetails': 函数调用缺少参数列表;使用 '&CreditCardPayment::paymentDetails' 创建指向成员的指针

代码是:

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

class Payment
{
private: float amount;

public: Payment(float=0.0);
        void paymentDetails();
        float getamount();
        void setamount(float);
};


Payment::Payment(float a)
{
    setamount(a);
}

void Payment::setamount(float a){amount=a;}
float Payment::getamount(){return amount;}

void Payment::paymentDetails()
{
    cout<<"The amount of payment is : "<<getamount<<"$"<<endl;
}

class cashPayment: public Payment
{
public: cashPayment(float=0.0);
        void paymentDetails();
};
cashPayment::cashPayment(float a):Payment(a){};
void cashPayment::paymentDetails()
{
    cout<<"The payment in cash is : "<<getamount()<<"$"<<endl;
}

class CreditCardPayment: public Payment
{
private: char* name;
         int creditnumber;
         int day,month,year;

public: CreditCardPayment(char[]=" ",int=0, int=0, int=0, int=0 ,float=0.0);
        void paymentDetails();
};

CreditCardPayment::CreditCardPayment(char* n, int cn, int d, int m, int y, float a):Payment(a)
{
    int l=strlen(n);
    name = new char[l+1];
    strncpy(name,n,l);
    name[l]='\0';
    creditnumber=cn;
    day=d;
    month=m;
    year=y;
}
void CreditCardPayment::paymentDetails()
{
    cout<<"Credit Card Holder Information & Payment: "<<endl;
    cout<<"Name is "<<name<<endl;
    cout<<"Credit Number is "<<creditnumber<<endl;
    cout<<"Expiration Date (Day / Month / Year) is "<<day<<"/"<<month<<"/"<<year<<endl;
    //cout<<"Payment is "<<Payment::getamount()<<"$"<<endl;
}

int main()
{
    CreditCardPayment cc1("Mohammad",936623,21,9,2011,3000);
    cashPayment cp1(4500);
    cp1.paymentDetails;
    cc1.paymentDetails;

    system("pause");
    return 0;
}
4

2 回答 2

1

在这里:

void Payment::paymentDetails()
{
    cout<<"The amount of payment is : "<<getamount<<"$"<<endl;
}

编译器告诉您,您的意思可能是getamount()而不是getamount.

同样在这里:

cp1.paymentDetails;
cc1.paymentDetails;

你可能的意思是:

cp1.paymentDetails();
cc1.paymentDetails();

与其他一些语言不同,在 C++ 中,当您调用不带参数的函数时,您仍然必须在括号中提供一个空参数列表。

于 2012-01-13T22:01:14.873 回答
1
cp1.paymentDetails;
cc1.paymentDetails;

错误状态为“函数调用缺少参数列表”。您的函数调用确实缺少参数列表。您需要使用()来调用函数:

cp1.paymentDetails();
cc1.paymentDetails();

The second half of the error, which reads "'&Payment::getamount' to create a pointer to member" is a "helpful" hint that if you meant to take the address of the function, you need to use the unary & (address-of) operator.

This hint is there because the operator is not required for computing the address of a non-member function (for compatibility with C and for legacy code), but it is required for computing the address of a nonstatic member function.

于 2012-01-13T22:04:19.833 回答