如何正确地实现一个抽象类?

时间:2022-09-02 11:20:37

I'm trying to create a simple abstract class so that multiple subclasses can implement a method.

我正在尝试创建一个简单的抽象类,以便多个子类可以实现一个方法。

My abstract class: Component.h

我的抽象类:Component.h

#ifndef COMPONENT_H
#define COMPONENT_H

class Component {

    public: 
        virtual void draw() = 0;

};
#endif

Class that implements: Instruction Memory.cpp

实现的类:指令存储器。cpp

#include <Component.h>
#include <GL/gl.h>
using namespace std;

class InstructionMemory : public Component {

    private:
        float width = 145;
        float height = 180;

    public:
        void Component::draw() {
            glBegin(GL_QUADS);
                glVertex2f(0, 0);
                glVertex2f(0, height);
                glVertex2f(width, height);
                glVertex2f(width, 0);
            glEnd();
        }
};

Right now, I'm getting an error: "cannot define member function 'Component::draw' within 'InstructionMemory.'"

现在,我得到了一个错误:“无法定义成员函数'Component:::绘制' within 'InstructionMemory '。”

As you can see, I'm trying to make an OpenGL project where each component can draw itself.

正如您所看到的,我正在尝试创建一个OpenGL项目,其中每个组件都可以自己绘制。

Edit: I thought if I included the abstract class, any classes that implement it would be ok. I'm getting that "'InstructionMemory' was not declared in this scope." Do I need to make an InstructionMemory.h? Here is my full code:

编辑:我想如果我包含了抽象类,任何实现它的类都可以。我得到"指令记忆"在这个范围内没有声明我需要做一个教学记忆吗?以下是我的全部代码:


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>

#include <Math.h>
#include <my_headers/Component.h>
using namespace std;

const int WIDTH = 1280;
const int HEIGHT = 720; 
Component* components[50];
int numComponents = 0;

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(1, 1, 1);

    glTranslatef(300, 300, 0);
    InstructionMemory mem;
    mem.draw();    // Here is where I want the memory unit to draw itself

    /* This will draw the memory unit. Copied and pasted from mem.draw()
    float width = 145;
    float height = 180;


    glBegin(GL_QUADS);
        glVertex2f(0, 0);
        glVertex2f(0, height);
        glVertex2f(width, height);
        glVertex2f(width, 0);
    glEnd();
    */


    glFlush();
}

void setup(void) {
   glClearColor(0.162, 0.181, 0.205, 1.0);
}

void resize(int w, int h) {
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, WIDTH, 0.0, HEIGHT, 0.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void keyInput(unsigned char key, int x, int y) {
   switch(key)
   {
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("CPU Simulator.cpp");
    glutDisplayFunc(display);
    glutReshapeFunc(resize);
    glutKeyboardFunc(keyInput);
    setup();
    glutMainLoop();
    return 0;
}

#ifndef COMPONENT_H
#define COMPONENT_H

class Component {

    public: 
        virtual void draw() = 0;

};
#endif

#include <my_headers/Component.h>
#include <GL/gl.h>
using namespace std;


class InstructionMemory : public Component {

    private:
        float width = 145;
        float height = 180;

    public:
        InstructionMemory();
        void draw() override {
            glBegin(GL_QUADS);
                glVertex2f(0, 0);
                glVertex2f(0, height);
                glVertex2f(width, height);
                glVertex2f(width, 0);
            glEnd();
        }
};

1 个解决方案

#1


5  

    void Component::draw() {

This is a scoped definition, you are trying to define the method Component::draw. It is legal to define it outside Component class but it's not legal to define it inside another class (InstructionMemory).

这是一个作用域定义,您正在尝试定义方法组件::draw。在组件类之外定义它是合法的,但是在另一个类(InstructionMemory)中定义它是不合法的。

You must remove the specifier Component:: and just let it be

您必须删除说明符组件::,让它继续

class InstructionMemory {
  ...
  void draw() override {

  }
};

or, if you want to define it outside the class:

或者,如果你想在类外定义它:

class InstructionMemory {
  void draw() override;
}

InstructionMemory::draw() {

}

#1


5  

    void Component::draw() {

This is a scoped definition, you are trying to define the method Component::draw. It is legal to define it outside Component class but it's not legal to define it inside another class (InstructionMemory).

这是一个作用域定义,您正在尝试定义方法组件::draw。在组件类之外定义它是合法的,但是在另一个类(InstructionMemory)中定义它是不合法的。

You must remove the specifier Component:: and just let it be

您必须删除说明符组件::,让它继续

class InstructionMemory {
  ...
  void draw() override {

  }
};

or, if you want to define it outside the class:

或者,如果你想在类外定义它:

class InstructionMemory {
  void draw() override;
}

InstructionMemory::draw() {

}