c++ - “X 类”没有成员 'Y'

标签 c++

此错误莫名其妙地发生。这是代码和输出:

定时器.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();
}

定时器.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();
};

编译器错误:

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

最佳答案

适合我。您确定您拥有正确的 timer.h 吗?试试这个:

cat timer.h

并验证它是否如您所想。如果是这样,请尝试在 .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

关于c++ - “X 类”没有成员 'Y',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2241944/

相关文章:

c++ - 编辑全局变量时减少重新编译

c++ - 使用 boost::beast 异步处理流式 HTTP

c++ - 标准中关于 "virtual function call in base constructor"的部分在哪里

c++ - 如何在成员函数线程中调用回调?

c++ - 完美转发到基类

c++ - 最终迭代时简单的 'for' 循环崩溃程序

c++ - 删除时出错 : expression must be a pointer to a complete object type

c++ - 检查所有可变参数模板参数的特征

c++ - 不同功能的容器?

C++构造函数问题(linux)