Reputation: 121
I am trying to create a simple program that uses an overloaded operator. I believe I have that part correct. But when I try to call my "getDollar" function in my "Money" class, I get "non-standard syntax; use '&' to create a pointer to a member" error. What am I missing?
Thanks for the help. (Code posted below)
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&);
};
Upvotes: 0
Views: 532
Reputation: 42858
You can't use a non-const lvalue reference to refer to a temporary.
Money& c
should be
Money c
Upvotes: 0
Reputation: 206667
To call getDollars
function on the object, you need to use the function call syntax. Instead of
cout << "\nThe amount of the two added together are "
<< c.getDollars << "." << c.getCents << endl;
use
cout << "\nThe amount of the two added together are "
<< c.getDollars() << "." << c.getCents() << endl;
// ^^ ^^
The compiler is not very helpful in guiding you to the solution. It's guessing that, maybe, you wanted to get a pointer to the member function and not make the function call. To get the a pointer to the member function, you'll need to use &Money::getDollars
.
Upvotes: 0
Reputation: 664
You are forming a pointer-to-member like so:
instance.function
However, the standard way of forming a pointer-to-member is like this:
&(instance.function)
Some compilers will silently accept your way, but most will at least complain about it, since it is technically non-portable.
However, I doubt you're actually trying to form a pointer-to-member. In that case, you just forgot the parenthesis around your functions in the cout statements.
Upvotes: 2