I’m implementing a Timer such that the time spent on different kinds of events are categorized and listed. The class declaration goes something as follows:
#include "timer_event.h"
#include <boost/timer.hpp>
#include <vector>
class Timer {
private:
class TimedEvent;
public:
static TimedEvent* Time(TimerEvent e);
protected:
private:
class TimedEvent {
public:
TimedEvent(double seconds, TimerEvent event);
~TimedEvent();
protected:
private:
TimerEvent event_;
double seconds_;
};
static boost::timer watch_;
};
Then, in the source file, I’m planning to implement the “Time” function as something like:
TimedEvent* Time(TimerEvent e) {
TimedEvent* ret = new TimedEvent(watch_.elapsed(),e);
return ret;
}
However, the compiler’s error message is:
../utils/timer.cc:24:1: error: ‘TimedEvent’ does not name a type
Could anyone try to help?
–Revised:
I’ve modified the Time function so that it now looks like:
Timer::TimedEvent* Timer::Time(TimerEvent e) {
TimedEvent* ret = new TimedEvent(watch_.elapsed(),e);
return ret;
}
However, since “Time” is declared as a static function in the class declaration. We need to declare the static objects in the cpp file since I’m getting this in the linker’s error message:
timer.cc:(.text+0x76): undefined reference to `Timer::TimedEvent::TimedEvent(double, TimerEvent)'
What kind of static object should I declare in the source file then?
That’s a compiler error, not a linker error.
Presumably your
Timewas supposed to look like this:I really dislike how everything starts with “Time”, by the way!
Now, inside the function you can probably use
TimedEventas it is. But the return type isn’t inside the function: you’ll have to qualify the type asTimer::TimedEventfor that:In addition, you need to define
Timer::TimedEvent‘s constructor somewhere.