【发布时间】:2020-04-04 00:04:56
【问题描述】:
我有 Hedgehog 类,它继承了 Body2D 类。我希望Hedgehog 使用默认的Body2D 绘图函数(它是render(SDL_Surface* )),因为在这种情况下它没有任何特定于类的行为,但我有一个错误:
||=== Build: Debug in SDLApples (compiler: GNU GCC Compiler) ===|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp||In function ‘int main(int, char**)’:|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|84|error: ‘void Body2D::render(SDL_Surface*)’ is inaccessible within this context|
engine/include/Body2D.h|37|note: declared here|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|84|error: ‘Body2D’ is not an accessible base of ‘Hedgehog’|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|85|error: ‘void Body2D::render(SDL_Surface*)’ is inaccessible within this context|
engine/include/Body2D.h|37|note: declared here|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|85|error: ‘Body2D’ is not an accessible base of ‘Apple’|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我的Hedgehog 类只是一个简单的实体,它从屏幕的一侧移动到另一侧并再次返回。这是source code。 Body2D 类是 2D 世界中的抽象体,可以绘制、更新并可以在同一 World 中的其他体中进行交互。这是Body2D 的header file 和source。这里是main 调用它们:
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include "Hedgehog.cpp"
#include "Apple.cpp"
#include "World.h"
#include <vector>
#define APPLE_COUNT 5
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
//------------------------------------------------------------------
//OBJECTS HERE
World scene;
Hedgehog hg(440, 280, scene);
std::vector<Apple> apples;
for(int i = 0; i < APPLE_COUNT; i++)
{
apples.push_back(Apple(i*100, -50, scene));
}
//------------------------------------------------------------------
// program main loop
bool done = false;
while (!done)
{
//======message processing loop======
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
if (event.key.keysym.sym == SDLK_ESCAPE) done = true;
if (event.key.keysym.sym == SDLK_RETURN) ; //Apples fall here
break;
}
} // end switch
} //======end of message processing=====
// UPDATE
hg.update();
for(Apple apple: apples) apple.update();
// RENDER
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
hg.render(screen);
for(Apple apple: apples) apple.render(screen);
SDL_Flip(screen);
} // end main loop
printf("Exited cleanly\n");
return 0;
}
所以问题是:我可以使用基类的函数而不覆盖它吗?还是只允许基类本身的对象使用默认行为?
附言我已经学习 Java 好几年了,但现在我必须使用 C++,所以我可能在理解继承的语言愿景方面犯了一些常见的 Java 程序员错误?
【问题讨论】:
-
要注意的一件快速的事情是,如果您只有一个变量,例如
Apple apple,那么该变量有它自己的数据副本。它不是对apples的引用。只需确保使用Apple const& apple或Apple & apple(取决于您是打算保持apple不变还是修改它)。或者auto&& apple如果你觉得懒惰。 -
这能回答你的问题吗? Default class inheritance access
-
未来:如果编译器给你一个错误,我可以向你保证其他人也有同样的问题。谷歌它!
-
@hegel5000 谢谢,很重要的通知,我会记住的!
-
@vasek 没有与结构进行比较的直接答案更清楚,但它作为扩展解释很有用,所以感谢它;)
标签: c++ inheritance