繁体   English   中英

“ X类”没有成员“ Y”

[英]'class X' has no member 'Y'

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

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();
};

编译器错误:

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

这似乎很奇怪

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();
}

为我编译好。

这表明编译器认为cTimer与标头中的内容不同。 因此,也许它是从另一个源文件中获取cTimer的定义的? 对于这种情况,您的“ timer.h”将不必正确地包含。 所以也许是错误的计时器。

一种检查方法是保存编译器预处理器的输出,并在其中搜索cTimer。

另一种选择是在您的timer.h中放入语法错误,并确保编译失败。

无论如何希望这会有所帮助

一些编译器有自己的timer.h,这是名称冲突。

还是其他奇怪的错误...

尝试将timer.h和timer.cpp重命名为更具描述性的名称,例如ClassTimer.h和ClassTimer.cpp,也许编译器正在链接另一个名为“ timer”的文件,因为它是一个非常通用的名称。 也可以在timer.cpp中尝试以下操作:


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

编辑:代码已编辑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM