我正在尝试调用一个函数。这些愚蠢的编译错误的原因是什么?
我正在尝试运行我编写的这个 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;
}
I'm trying to run this c++ program I wrote. I'm getting the following errors in Visual C++:
1>c:\users\server\desktop\problem7\problem7\main.cpp(26) : error C3867: 'Payment::getamount': function call missing argument list; use '&Payment::getamount' to create a pointer to member
1>c:\users\server\desktop\problem7\problem7\main.cpp(74) : error C3867: 'cashPayment::paymentDetails': function call missing argument list; use '&cashPayment::paymentDetails' to create a pointer to member
1>c:\users\server\desktop\problem7\problem7\main.cpp(75) : error C3867: 'CreditCardPayment::paymentDetails': function call missing argument list; use '&CreditCardPayment::paymentDetails' to create a pointer to member
the code is:
#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<<"quot;<<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()<<"quot;<<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()<<"quot;<<endl;
}
int main()
{
CreditCardPayment cc1("Mohammad",936623,21,9,2011,3000);
cashPayment cp1(4500);
cp1.paymentDetails;
cc1.paymentDetails;
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里:
编译器告诉您,您可能指的是
getamount()
而不是getamount
。此处类似:
您可能的意思是:
与其他一些语言不同,在 C++ 中,当您调用不带参数的函数时,您仍然必须在括号中提供空参数列表。
In here:
the compiler is telling you that you might have meant
getamount()
instead ofgetamount
.Similarly here:
you probably meant:
Unlike some other languages, in C++ when you call a function that takes no arguments you must still provide an empty argument list in parentheses.
该错误指出“函数调用缺少参数列表”。您的函数调用确实缺少参数列表。您需要使用
()
来调用函数:错误的后半部分显示“'&Payment::getamount' to create a point to member”,这是一个“有用”的提示如果您想获取函数的地址,则需要使用一元
&
(地址)运算符。之所以存在此提示,是因为计算非成员函数的地址不需要该运算符(为了与 C 兼容和遗留代码),但计算非静态成员的地址需要该运算符功能。
The error states "function call missing argument list." Your function calls are indeed missing the argument lists. You need to use
()
to call the functions: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.