tags:

views:

263

answers:

4

This error is inexplicably occurring. Here is the code and output:

timer.cpp:

#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"

void cTimer::recordCurrentTime()
{
    this->previous_t = this->current_t;
    this->current_t = SDL_GetTicks();
}

timer.h:

#include "SDL.h"
#include "SDL_timer.h"

class cTimer
{
private:
    int previous_t;
    int current_t;
    float delta_time;
    float accumulated_time;
    int frame_counter;
public:
    void recordCurrentTime();
    float getDelta();
    void incrementAccumulator();
    void decrementAccumulator();
    bool isAccumulatorReady();
    void incrementFrameCounter();
    void resetFrameCounter();
    int getFPS();
};

Compiler errors:

make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1

Compiler errors after removing the #include "timer.h"

g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
+6  A: 

Works for me. Are you sure you've got the right timer.h? Try this:

cat timer.h

and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:

[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
John Feminella
I bet this is the answer. :)
John Weldon
Yeah, adding ^__^ did nothing. But oddly enough, cat timer.h printed out the exact correct file.
revenantphoenix
However, it does seem to be ignoring the timer.h
revenantphoenix
rename your timer.h to something like mytimer.h
John Weldon
John, I just did that, to no effect.
revenantphoenix
You we're correct, it was using a timer.h.gch I was unaware of. Thanks.
revenantphoenix
Don't forget to run `make clean` before you build things! :)
John Feminella
You can also use `-MD` with GCC or `/showIncludes` with MSVC to help diagnose these kinds of problems.
Michael Burr
+3  A: 

This seems very odd as

class cTimer
{
private:
    int previous_t;
    int current_t;
    float delta_time;
    float accumulated_time;
    int frame_counter;
public:
    void recordCurrentTime();
    float getDelta();
    void incrementAccumulator();
    void decrementAccumulator();
    bool isAccumulatorReady();
    void incrementFrameCounter();
    void resetFrameCounter();
    int getFPS();
};
void cTimer::recordCurrentTime()
{
    this->previous_t = this->current_t;
    this->current_t = SDL_GetTicks();
}

Compiles OK for me.

This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.

A way to check this would be to save the compiler preprocessor output and search that for cTimer.

Another option might be to put a syntax error in your timer.h and make sure the compile fails.

Anyway hope this helps

Michael Anderson
good idea too, but I expect the issue is the include path / which timer.h is being included.
John Weldon
the include path is for SDL
revenantphoenix
+1  A: 

Some compilers have their own timer.h, this is a name conflict.

Or it is a something else of bizarre bug...

speeder
I tried renaming to cTimer.cpp and cTimer.h, no effect.
revenantphoenix
mind you that "cTimer is a contraction of "class and Timer" and that is common as you naming a person "John" or a dog as "Rex".Test naming it like: MyNameClassTimer and see if it fixes. It it does not fix, then try recompiling the entire project from scratch
speeder
Btw: What I mean by "MyName" is really your name, I for example name my classes that have name collision as HMGSomething (HMG are my initials)
speeder
+1  A: 
Dooms101
This won't work for me, it'll just give a 'multiple declaration' error.
revenantphoenix
Sorry I was thinking of something else in Objective-C... well the edited code works for me. I think you have a name confliction, maybe try using a namespace and a different file name?
Dooms101