2015-11-04 85 views
0

我想创建一个使用重载操作符的简单程序。我相信我有这个部分是正确的。但是,当我尝试在我的“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.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&); 
}; 
+0

你根本都忘了'后和getDollars''getCents'括号中'cout'线。 –

+0

谢谢!这解决了它。 –

回答

2

您正在形成一个指针到构件像这样:

instance.function 

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

&(instance.function) 

一些编译器会默默接受的方式,但大多数至少会抱怨,因为它在技术上是不便携。

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

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

0

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

Money& c 

应该

Money c 
相关问题