我正在尝试调用一个函数。这些愚蠢的编译错误的原因是什么?

发布于 2024-12-27 02:18:35 字数 2509 浏览 1 评论 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;
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2025-01-03 02:18:35

在这里:

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

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

此处类似:

cp1.paymentDetails;
cc1.paymentDetails;

您可能的意思是:

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

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

In here:

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

the compiler is telling you that you might have meant getamount() instead of getamount.

Similarly here:

cp1.paymentDetails;
cc1.paymentDetails;

you probably meant:

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

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.

谁的新欢旧爱 2025-01-03 02:18:35
cp1.paymentDetails;
cc1.paymentDetails;

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

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

错误的后半部分显示“'&Payment::getamount' to create a point to member”,这是一个“有用”的提示如果您想获取函数的地址,则需要使用一元 & (地址)运算符。

之所以存在此提示,是因为计算非成员函数的地址不需要该运算符(为了与 C 兼容和遗留代码),但计算非静态成员的地址需要该运算符功能。

cp1.paymentDetails;
cc1.paymentDetails;

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文