Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'must have an argument of class or enumerated type' actually mean

Tags:

c++

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

like image 825
user1082764 Avatar asked Jun 17 '13 02:06

user1082764


1 Answers

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;
like image 112
David G Avatar answered Oct 04 '22 02:10

David G