简体   繁体   English

“ X类”没有成员“ Y”

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

This error is inexplicably occurring. 此错误莫名其妙地发生。 Here is the code and output: 这是代码和输出:

timer.cpp: 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: 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" 删除#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

Works for me. 为我工作。 Are you sure you've got the right timer.h ? 您确定您有正确的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. 如果是这样,请尝试在.h文件的开头添加^__^ ,以查看是否出现语法错误。 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

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. 这表明编译器认为cTimer与标头中的内容不同。 So maybe its getting a definition of cTimer from another source file? 因此,也许它是从另一个源文件中获取cTimer的定义的? For this to be the case your "timer.h" would have to not be gettting included correctly. 对于这种情况,您的“ timer.h”将不必正确地包含。 So maybe the wrong timer.h. 所以也许是错误的计时器。

A way to check this would be to save the compiler preprocessor output and search that for cTimer. 一种检查方法是保存编译器预处理器的输出,并在其中搜索cTimer。

Another option might be to put a syntax error in your timer.h and make sure the compile fails. 另一种选择是在您的timer.h中放入语法错误,并确保编译失败。

Anyway hope this helps 无论如何希望这会有所帮助

Some compilers have their own timer.h, this is a name conflict. 一些编译器有自己的timer.h,这是名称冲突。

Or it is a something else of bizarre bug... 还是其他奇怪的错误...

Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. 尝试将timer.h和timer.cpp重命名为更具描述性的名称,例如ClassTimer.h和ClassTimer.cpp,也许编译器正在链接另一个名为“ timer”的文件,因为它是一个非常通用的名称。 Also try this in timer.cpp: 也可以在timer.cpp中尝试以下操作:


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

Edit: code edited 编辑:代码已编辑

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

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