简体   繁体   English

在函数中使用继承的类?

[英]Using an inherited class in a function?

Answered: Thanks to @AndyProwl pointing out some stuff to me, I found out the problem was in my ScreenImage.Update() function. 回答:感谢@AndyProwl向我指出了一些东西,我发现问题出在我的ScreenImage.Update()函数中。 I had flopped back and forth on writing implementation for that function (as the ScreenImage doesn't need to Update, it just needs to be drawn) and somehow accidentally left the Update function on my interface when I erased the implementation... 我在为该功能编写实现时来回翻了一下(因为ScreenImage不需要更新,只需要绘制它),当我删除实现时,不知何故在我的界面上留下了Update函数...

LET THAT BE A WARNING TO YOU KIDS. 让我们警告您的孩子。 ~The more you know~ 〜了解更多〜

Original Question: 原始问题:

I'm making a game, and I have a Screen object that keeps track of items on a given screen (ie images, text, menu options, etc.). 我正在做一个游戏,并且有一个Screen对象,该对象可以跟踪给定屏幕上的项目(即图像,文本,菜单选项等)。 My goal is to pass these items to the Screen in the form of an abstract ScreenElements object that could, in turn, be defined as more concrete objects, like a ScreenImage or ScreenText etc. 我的目标是将这些项目以抽象的ScreenElements对象的形式传递给Screen ,该对象又可以定义为更具体的对象,例如ScreenImageScreenText等。

I went through a headache trying to get the vector (basically treating it as a stack for ScreenElements) to play nice with the different inherited classes by making it a vector of pointers to the base object, but then that means I have to declare objects inheriting from ScreenElements outside of a function so the pointer doesn't become invalid when the declaration goes out of scope. 我很头疼,试图通过使向量成为指向基础对象的指针向量来使向量(基本上将其作为ScreenElement的堆栈使用)与不同的继承类一起很好地工作,但是那意味着我必须声明继承的对象从ScreenElements外部的ScreenElements ,因此当声明超出范围时,指针不会变得无效。 So then I made a function in my Screen object to allow me to "AddElement" and I thought the same pointer method would work... it apparently doesn't. 因此,然后我在Screen对象中创建了一个函数以允许我“ AddElement”,并且我认为相同的指针方法可以工作……显然不行。

Here's my source code just to clear things up. 这是我的源代码,目的只是为了清理问题。 This is Screen.h. 这是Screen.h。

#ifndef SCREEN_H
#define SCREEN_H

#include <vector>

#include "ScreenElement.h"

class Screen
{
protected:
    bool enabled;

    std::vector<ScreenElement*> screen_elements;
public:
    //Gameloop Functions
    void Init();
    void Update(int ticks); //Enabled will likely determine if this is called
    void Draw(); //And this perhaps on a visible bool
    void Destroy();

    //Concept functions
    //void TransitionIn(data);
    //void TransitionOut(data);

    void AddElement(ScreenElement *Element); //function in question

    //Allow external use of enabled feature
    void ToggleEnable(bool Enabled) { enabled = Enabled; }
    bool IsEnabled() { return enabled; }
};

#endif // SCREEN_H

And my Screen.cpp file 还有我的Screen.cpp文件

#include "Screen.h"

void Screen::Init()
{
    enabled = true;
}

void Screen::Update(int ticks)
{
    for(int i = 0; i < screen_elements.size(); i++)
        screen_elements[i]->Update(ticks);
}

void Screen::Draw()
{
    for(int i = 0; i < screen_elements.size(); i++)
        screen_elements[i]->Draw();
}

void Screen::Destroy()
{
    screen_elements.clear();
}

void Screen::AddElement(ScreenElement *Element)
{
    screen_elements.push_back(Element);
}

I need my AddElement to work with any inherited form of ScreenElement, such as ScreenImage for instance, and store it into the vector. 我需要我的AddElement来处理ScreenElement的任何继承形式,例如ScreenImage,并将其存储到向量中。

EDIT: I guess I should also put up some source code from my ScreenElement class too. 编辑:我想我也应该从ScreenElement类中放一些源代码。 When I attempted to add a virtual destructor it only caused another error in addition to the first which seems to be related to the inherited class's constructor (In function ScreenImage::ScreenImage() undefined reference to 'vtable for ScreenImage'). 当我尝试添加虚拟析构函数时,除了第一个似乎与继承的类的构造函数有关的错误外,它仅引起了另一个错误(在ScreenImage :: ScreenImage()函数中未定义对“ vTable for ScreenImage”的引用)。 So here's my ScreenElement.h 所以这是我的ScreenElement.h

#ifndef SCREENELEMENT_H
#define SCREENELEMENT_H

#include "Globals.h"

class ScreenElement
{
protected:
    s_Position screen_position;

    bool enabled;
public:
    virtual void Update(int ticks) {}
    virtual void Draw() {}
    virtual void Destroy() {}

    void ToggleEnable(bool Enabled) { enabled = Enabled; }
    bool IsEnabled() { return enabled; }
};

#endif // SCREENELEMENT_H 

Which in turn was used to create ScreenImage.h 依次用来创建ScreenImage.h

#ifndef SCREENIMAGE_H
#define SCREENIMAGE_H

#include "Globals.h"
#include "ScreenElement.h"
#include "SpriteSheet.h"

class ScreenImage: public ScreenElement
{
private:
    SpriteSheet *sprite_sheet;

    int sprite_sheet_frame;
public:
    void Init(SpriteSheet *Sprite_Sheet, float X, float Y, int SpriteSheetFrame = 0);
    void Update(int ticks);
    void Draw();
    void Destroy();
};

#endif // SCREENIMAGE_H

And has its own implementations for base functions in ScreenImage.cpp. 并且在ScreenImage.cpp中具有针对基本功能的自己的实现。

After initializing an instance of Screen called SplashScreen and a ScreenImage called SplashScreen_Image, I use "SplashScreen.AddElement(&ScreenImage);" 在初始化名为SplashScreen的Screen实例和名为SplashScreen_Image的ScreenImage实例之后,我使用“ SplashScreen.AddElement(&ScreenImage);”。 Then it throws me an error about having 'undefined reference to "vtable for ScreenImage" 然后,它抛出一个关于“对“ vImage for ScreenImage”的未定义引用”的错误

Judging from the error you are reporting, the problem seems to be that you have provided a declaration, but not a definition , for some virtual function of your class. 从报告的错误来看,问题似乎是您为类的某些虚函数提供了声明,但没有提供定义

For instance, trying to compile the following code will produce a very similar error: 例如,尝试编译以下代码将产生非常相似的错误:

struct X
{
    virtual void foo() { }
};

struct Y : X
{
    virtual void foo(); // Missing definition...
};

int main()
{
    Y y;
    y.foo(); // The compiler will complain about undefined reference
             // to vtable for Y...
}

Here is the corresponding live example . 这是相应的实时示例

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

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