了解重载运算符。收到"non-standard syntax; use '&' to create a pointer to a member"错误

Learning about Overloading Operators. Getting "non-standard syntax; use '&' to create a pointer to a member" error

本文关键字:to 错误 create member pointer use 收到 non-standard syntax 运算符 了解      更新时间:2023-10-16

我正在尝试创建一个使用重载运算符的简单程序。我相信我的那部分是正确的。但是,当我尝试在"Money"类中调用"getDollar"函数时,会出现"非标准语法;使用'&'创建指向成员的指针"错误。我错过了什么?

谢谢你的帮助。(代码张贴在下面)

驱动程序.cpp

#include <iostream>
#include "Money.h"
using namespace std;
// Declaring Variables
Money a;
Money b;
int cents;
int dollars;
// Main Function
int main()
{
    cout << "Please enter in a monetary value:n" << "Dollars: ";
    cin >> dollars;
    cout << "nCents: ";
    cin >> cents;
    Money a(dollars, cents);
    cout << "Please enter in second monetary value:n" << "Dollars: ";
    cin >> dollars;
    cout << "nCents: ";
    cin >> cents;
    Money b(dollars, cents);
    Money& c = a + b;
    cout << "nThe amount of the two added together are " << c.getDollars << "." << c.getCents << endl;
    c = a - b;
    cout << "nThe amount of the first value subtracted by the second value is " << c.getDollars << "." << c.getCents << endl;

    system("PAUSE");
    return 0;
}

Money.cpp

#include "Money.h"
#include <iostream>
using namespace std;

Money::Money()
{
    dollars = 0;
    cents = 0;
}

Money::Money(int d, int c)
{
    dollars = d;
    cents = c;
}
int Money::getDollars() const
{
    return dollars;
}
int Money::getCents() const
{
    return cents;
}
Money Money::operator+ (const Money& otherMoney)
{
    // Declare a new "Money" object
    Money newMoney;
    // Add the cents from money object 1 and money object 2
    newMoney.cents = cents + otherMoney.cents;
    // Add the dollars from money object 1 and money object 2
    newMoney.dollars = dollars + otherMoney.dollars;
    // Return the new money object
    return newMoney;
}
Money Money::operator- (const Money& otherMoney)
{
    // Declare a new "Money" object
    Money newMoney;
    // Subtract the cents of money object to FROM money object 1
    newMoney.cents = cents - otherMoney.cents;
    // Subtract the dollars of money object to FROM money object 1
    newMoney.dollars = dollars - otherMoney.dollars;
    // Return the new money object
    return newMoney;
}

Money.h

#pragma once
#include <iostream>
using namespace std;
class Money
{
private:
    int dollars;
    int cents;
public:
    // Default Constructor
    // Purpose: Remove any data
    // Parameter: Void
    // Return: None
    Money();
    // Parameterized Constructor
    // Purpose: Set dollars and cents equal to "d" and "c"
    // Parameter: two ints, "d" and "c"
    // Return: None
    Money(int, int);
    // getDollars Function
    // Purpose: Returns dollars
    // Parameter: None
    // Return: int (dollars)
    int getDollars() const;
    // getCents Function
    // Purpose: Returns cents
    // Parameter: None
    // Return: int (cents)
    int getCents() const;
    // + Operator Overload
    // Purpose: Add the money of two different Money Objects together
    // Parameter: Money Object
    // Return: Money Object
    Money operator+ (const Money&);
    // - Operator Overload
    // Purpose: Subtract the money of one Money object from another
    // Parameter: Money Object
    // Return: Money Object
    Money operator- (const Money&);
};

您正在形成一个指向成员的指针,如下所示:

instance.function

然而,形成指向成员的指针的标准方式是这样的:

&(instance.function)

有些编译器会默默地接受你的方式,但大多数编译器至少会抱怨它,因为它在技术上是不可移植的。

然而,我怀疑你实际上是在试图形成一个指向成员的指针。在这种情况下,您只是忘记了cout语句中函数周围的括号。

要在对象上调用getDollars函数,需要使用函数调用语法。代替

cout << "nThe amount of the two added together are "
     << c.getDollars << "." << c.getCents << endl;

使用

cout << "nThe amount of the two added together are "
     << c.getDollars() << "." << c.getCents() << endl;
     //             ^^                     ^^

编译器在引导您找到解决方案方面没有太大帮助。它猜测,也许,您想要获得一个指向成员函数的指针,而不是进行函数调用。要获得指向成员函数的指针,您需要使用&Money::getDollars

不能使用非常量左值引用来引用临时值。

Money& c

应该是

Money c