我正在尝试运行我写的这个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;
}
答案 0 :(得分:1)
在这里:
void Payment::paymentDetails()
{
cout<<"The amount of payment is : "<<getamount<<"$"<<endl;
}
编译器告诉您,您的意思可能是getamount()
而不是getamount
。
同样在这里:
cp1.paymentDetails;
cc1.paymentDetails;
你可能意味着:
cp1.paymentDetails();
cc1.paymentDetails();
与其他一些语言不同,在C ++中调用不带参数的函数时,你必须在括号中提供一个空参数列表。
答案 1 :(得分:1)
cp1.paymentDetails;
cc1.paymentDetails;
错误表示“函数调用缺少参数列表”。您的函数调用确实缺少参数列表。您需要使用()
来调用函数:
cp1.paymentDetails();
cc1.paymentDetails();
错误的后半部分,其中显示“'&amp; Payment :: getamount'以创建指向成员的指针”是一个“有用的”提示,如果您打算获取该函数的地址,则需要使用一元&
(地址)运算符。
这个提示是因为运算符不需要计算非成员函数的地址(为了与C和遗留代码兼容),但它是计算地址所必需的一个非静态成员函数。