I have a header file and a .cpp file. I an needing to write functions for my .h file but i get an error before i can fully complete a skeleton .cpp file.
Money.h
#ifndef MONEY_H
#define MONEY_H
#include <iostream>
#include <iomanip>
using namespace std;
class Money
{
public:
Money(int dollars, int cents);
Money operator+(const Money& b) const;
Money operator-(const Money& b) const;
Money operator*(double m) const;
Money operator/(double d) const;
void print() const;
private:
int dollars;
int cents;
};
#endif
Money.cpp
#include "Money.h"
Money::Money(int dollars, int cents){
}
Money operator+(const Money& b) {
}
Money operator-(const Money& b) {
}
Money operator*(double m) {
}
Money operator/(double d) {
}
void print(){
}
The errors are with the multiply and divide operators:
Money.cpp:12:25: error: 'Money operator*(double)' must have an argument of class or enumerated type
Money.cpp:15:25: error: 'Money operator/(double)' must have an argument of class or enumerated type
You're not using the scope resolution operator to tell the compiler that you are defining a member function. It is instead interpreted as a global operator overload, which takes two arguments, one of which must be of class or enumerated type. This basically means that one of your arguments must either be a user-defined type (type that is not a primitive type) or an enumerated type which is defined through an enum
.
In your original code Money
is just the return type; it doesn't tell the compiler that you are defining member function from that class.
Here is a fix for one of your lines:
Money Money::operator+(const Money& b) /*
^^^^^^^ */
{
// ...
}
Moreover, your prototypes and definitions must also match in cv-qualification. Your definitions were missing the const
qualifier...
Money Money::operator+(const Money& b) const /*
^^^^^ */
{
// ...
}
Update:
I also found that your definition for Money::operator*
and Money::operator/
do not match their prototypes. The prototypes for both take a double
while the definitions take Money const&
. You will need to change one to match the other.
// inside Money class
Money operator*(Money const&) const;
Money operator/(Money const&) const;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With