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

C++ 学习重载运算符。获得;非标准语法;使用'&';创建指向成员的指针";错误,c++,pointers,reference,operator-overloading,C++,Pointers,Reference,Operator Overloading,我试图创建一个使用重载运算符的简单程序。我相信我的那部分是正确的。但是当我试图在我的“Money”类中调用我的“getDollar”函数时,我得到了“非标准语法;使用“&”创建指向成员的指针”错误。我错过了什么 谢谢你的帮助。(代码如下) Driver.cpp #include <iostream> #include "Money.h" using namespace std; // Declaring Variables Money a; Money b; int cents

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

谢谢你的帮助。(代码如下)

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.h”
使用名称空间std;
//声明变量
金钱;
钱b;
整数美分;
整数美元;
//主要功能
int main()
{
库特美元;
不能>美分;
货币a(美元,美分);
库特美元;
不能>美分;
货币b(美元,美分);
货币&c=a+b;

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

instance.function
但是,形成成员指针的标准方法如下:

&(instance.function)
有些编译器会默默地接受您的方式,但大多数编译器至少会抱怨它,因为它在技术上是不可移植的


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

要调用对象上的
getDollars
函数,您需要使用函数调用语法。而不是

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

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

Money& c
应该是

Money c

您只是忘记了
cout
行中
getDollars
getCents
后面的大括号。谢谢!这就解决了问题。
cout << "\nThe amount of the two added together are "
     << c.getDollars() << "." << c.getCents() << endl;
     //             ^^                     ^^
Money& c
Money c