'X级'没有成员'Y'

rev*_*nix 1 c++

这个错误是莫名其妙地发生的.这是代码和输出:

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();
}
Run Code Online (Sandbox Code Playgroud)

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();
};
Run Code Online (Sandbox Code Playgroud)

编译器错误:

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

删除#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

Joh*_*lla 6

适合我.你确定你有权利timer.h吗?试试这个:

cat timer.h
Run Code Online (Sandbox Code Playgroud)

并验证它是你认为的.如果是这样,请尝试^__^在.h文件的开头添加并查看是否出现语法错误.它应该看起来像这样:

[/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
Run Code Online (Sandbox Code Playgroud)