了解重载运算符。获得"非标准语法;使用'&'创建指向成员的指针"错误

时间:2015-11-04 06:10:58

标签: c++ pointers reference operator-overloading

我正在尝试创建一个使用重载运算符的简单程序。我相信我有那个部分是正确的。但当我试着打电话给我的" getDollar"功能在我的" Money"我得到"非标准语法;使用'&'创建指向成员的指针"错误。我错过了什么?

感谢您的帮助。 (下面的代码)

Driver.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&);
};

3 个答案:

答案 0 :(得分:2)

你正在形成一个指向成员的指针:

instance.function

但是,形成指向成员的标准方法是这样的:

&(instance.function)

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

但是,我怀疑你是否真的试图形成指向成员的指针。在这种情况下,您只是忘记了cout语句中函数的括号。

答案 1 :(得分:0)

要在对象上调用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

答案 2 :(得分:0)

您不能使用非const左值引用来引用临时值。

Money& c

应该是

Money c