如何正确实现抽象类?

时间:2015-12-02 22:05:36

标签: c++ opengl

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

我的抽象类:Component.h

#ifndef COMPONENT_H
#define COMPONENT_H

class Component {

    public: 
        virtual void draw() = 0;

};
#endif

实现的类:指令Memory.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();
        }
};

现在,我收到错误:&#34;无法定义成员函数&#39; Component :: draw&#39;在&#39; InstructionMemory。&#39;&#34;

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

编辑:我想如果我包含抽象类,任何实现它的类都可以。我得到了#34;&#39; InstructionMemory&#39;未在此范围内声明。&#34;我需要制作一个InstructionMemory.h吗?这是我的完整代码:

#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 个答案:

答案 0 :(得分:5)

    void Component::draw() {

这是一个范围定义,您正在尝试定义方法Component::draw。在Component类之外定义它是合法的,但在另一个类(InstructionMemory)中定义它是不合法的。

您必须删除说明符Component::,然后将其设为

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

  }
};

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

class InstructionMemory {
  void draw() override;
}

InstructionMemory::draw() {

}