Reputation: 233
I'm trying to create a simple abstract class so that multiple subclasses can implement a method.
My abstract class: Component.h
#ifndef COMPONENT_H
#define COMPONENT_H
class Component {
public:
virtual void draw() = 0;
};
#endif
Class that implements: Instruction 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();
}
};
Right now, I'm getting an error: "cannot define member function 'Component::draw' within 'InstructionMemory.'"
As you can see, I'm trying to make an OpenGL project where each component can draw itself.
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();
}
};
Upvotes: 1
Views: 103
Reputation: 133599
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
).
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() {
}
Upvotes: 5