1

It's not often that I work in C++ and I've run into a simple error that is holding me up.

In Xcode I have the following two errors: In Event.h: Control reaches end of non-void function In Event.cpp: Overloaded operator must have at least one argument of class or enumeration

Both errors are at the lines of the method signature for

bool operator () (Event *left, Event *right)

Here are the .h and .cpp files in their entirety (not that much going on yet): Event.h

#ifndef __EventSimulation__EventComparison__
#define __EventSimulation__EventComparison__

#include <iostream>
#include "Event.h"
class EventComparison {
public:
    bool operator () (Event *left, Event *right){}

};
#endif

Event.cpp

#include "EventComparison.h"
#include "Event.h"

bool operator() (Event *left, Event *right) {
    return left->time > right->time;
}

Can someone please help me fix this error and explain what/why things are issuing a compile error and how to avoid this in the feature. Thanks for your help!

Matt
  • 879
  • 9
  • 29

2 Answers2

6

Change your header Event.h to

class EventComparison {
public:
    // the {} is the body of the function, therefore
    // you added a function defintion, though you did
    // not return a result
    // bool operator () (Event *left, Event *right){}

    // this is a function declaration:
    // the body of the function is not defined
    bool operator () (Event *left, Event *right);
};

What you did in the header is actualy define the function by adding the parenthesis.

Then in the source file do

bool EventComparison::operator() (Event *left, Event *right) {
     return left->time > right->time;
}

You defined a bool operator in global namespace, but what you wanted to do is define a member function. For this to happen you have to specify which class the function belongs to, which you can do via the EventComparison:: part.

mfuchs
  • 2,190
  • 12
  • 20
  • The error has been removed from the header file with the change, however, the implementation file still has the error. – Matt Feb 24 '15 at 19:58
  • I added the namespace of EventComparision and this resolved the issue, but I don't understand why I have to if the method appears in the EventComparison.cpp file. Isn't the name space implied? How could I resolve without adding the namespace – Matt Feb 24 '15 at 20:02
  • `EventComparison` is not a namespace. It is the class. – mfuchs Feb 24 '15 at 20:03
  • Thanks for the clarification. Is there away to move the class name out of the individual method so it applies to all the method in EventComparison.cpp without getting a duplication error? – Matt Feb 24 '15 at 20:19
  • You mean you want to avoid constantly writing `EventComparison` in Event.cpp? No that is not possible. You could define your functions inline in the header. See for example [here](http://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header?lq=1) and [here](http://stackoverflow.com/questions/1932311/when-to-use-inline-function-and-when-not-to-use-it?lq=1) for more information on inline. – mfuchs Feb 24 '15 at 20:23
2
bool operator () (Event *left, Event *right){}

defines a member function which does nothing. Such a function would have to have return type void, since it doesn't return anything.

On the other hand, your definition of the operator doesn't indicate that it is a class member.

In short, you need:

// Declaration
class EventComparison {
  public:
    // NOTE: const
    bool operator () const (Event *left, Event *right);
};

// Implementation
bool EventComparison::operator() const (Event *left, Event *right) {
      return left->time > right->time;
}
rici
  • 234,347
  • 28
  • 237
  • 341