Hi i am mostly done with my assignment where u have to make a simple object viewer. one of the requirements require you to use multiple instantiation of objects from the same class which i do not know what to do about it. This is my code for main.cpp

#include <iostream>
#include "freeglut.h"
#include "main_class.h"
#include "cube.h"
#include "pyramid.h"
#include "cuboid.h"

using namespace std;

int speed;

void MyDisplay (void);
void init (void) {
	glClearColor (0.0, 0.0, 0.0, 0.0);	// set background colour to black
	glEnable (GL_CULL_FACE);
	glShadeModel (GL_FLAT);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective (50.0, 1.45, 1.0, 1000.0);
	glMatrixMode (GL_MODELVIEW);

	gluLookAt (100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void rescale (GLsizei w, GLsizei h) {
	glViewport (0, 0, w, h);
}

void cube :: render (void)
{
	glBegin (GL_QUADS);
	//red
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f (-10.0, 10.0, 10.0);
		glVertex3f (-10.0, -10.0, 10.0);
		glVertex3f (10.0, -10.0, 10.0);
		glVertex3f (10.0, 10.0, 10.0);
	//green
		glColor3f (0.0, 1.0, 0.0);
		glVertex3f (10.0, 10.0,10.0);
		glVertex3f (10.0, -10.0, 10.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (10.0, 10.0, -10.0);
	//blue
		glColor3f (0.0, 0.0, 1.0);
		glVertex3f (-10.0, 10.0, 10.0);
		glVertex3f (10.0, 10.0, 10.0);
		glVertex3f (10.0, 10.0, -10.0);
		glVertex3f (-10.0, 10.0, -10.0);
	//yellow
		glColor3f (1.0, 1.0, 0.0);
		glVertex3f (-10.0, 10.0, -10.0);
		glVertex3f (10.0, 10.0, -10.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (-10.0, -10.0, -10.0);

		glColor3f (0.0, 1.0, 1.0);
		glVertex3f (-10.0, -10.0, -10.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (10.0, -10.0, 10.0);
		glVertex3f (-10.0, -10.0, 10.0);

		glColor3f (1.0, 0.0, 1.0);
		glVertex3f (-10.0, 10.0, 10.0);
		glVertex3f (-10.0, 10.0, -10.0);
		glVertex3f (-10.0, -10.0, -10.0);
		glVertex3f (-10.0, -10.0, 10.0);
	glEnd ();
}

void cuboid::render(void)
{
	glBegin (GL_QUADS);
	//red
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f (-10.0, 10.0, 20.0);
		glVertex3f (-10.0, -10.0, 20.0);
		glVertex3f (10.0, -10.0, 20.0);
		glVertex3f (10.0, 10.0, 20.0);
	//green
		glColor3f (0.0, 1.0, 0.0);
		glVertex3f (10.0, 10.0,20.0);
		glVertex3f (10.0, -10.0, 20.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (10.0, 10.0, -10.0);
	//blue
		glColor3f (0.0, 0.0, 1.0);
		glVertex3f (-10.0, 10.0, 20.0);
		glVertex3f (10.0, 10.0, 20.0);
		glVertex3f (10.0, 10.0, -10.0);
		glVertex3f (-10.0, 10.0, -10.0);
	//yellow
		glColor3f (1.0, 1.0, 0.0);
		glVertex3f (-10.0, 10.0, -10.0);
		glVertex3f (10.0, 10.0, -10.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (-10.0, -10.0, -10.0);
	//cyan
		glColor3f (0.0, 1.0, 1.0);
		glVertex3f (-10.0, -10.0, -10.0);
		glVertex3f (10.0, -10.0, -10.0);
		glVertex3f (10.0, -10.0, 20.0);
		glVertex3f (-10.0, -10.0, 20.0);
	//purple
		glColor3f (1.0, 0.0, 1.0);
		glVertex3f (-10.0, 10.0, 20.0);
		glVertex3f (-10.0, 10.0, -10.0);
		glVertex3f (-10.0, -10.0, -10.0);
		glVertex3f (-10.0, -10.0, 20.0);
	glEnd ();
}

void pyramid::render(void)
{
	glBegin (GL_TRIANGLES);
	//red triangle 
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f (5.0, 0.0, 0.0);
		glVertex3f(15.0, 0.0, 0.0);
		glVertex3f(10.0, 10.0, -5.0);

	//green triangle
		glColor3f(0.0, 1.0, 0.0);
		glVertex3f(10.0, 10.0, -5.0);
		glVertex3f (15.0, 0.0, 0.0);
		glVertex3f(15.0, 0.0, -10.0);
	
	//blue triangle
		glColor3f(0.0, 0.0, 1.0);
		glVertex3f(10.0, 10.0, -5.0);
		glVertex3f(15.0, 0.0, -10.0);
		glVertex3f(5.0, 0.0, -10.0);

	//yellow triangle
		glColor3f(1.0, 1.0, 0.0);
		glVertex3f(10.0, 10.0, -5.0);
		glVertex3f(5.0, 0.0, -10.0);
		glVertex3f(5.0, 0.0, 0.0);
	
		glEnd ();

		glBegin(GL_QUADS);

	//white square
		glColor3f(1.0, 1.0, 1.0);
		glVertex3f(5.0, 0.0, -10.0);
		glVertex3f(15.0, 0.0, -10.0);
		glVertex3f(15.0, 0.0, 0.0);
		glVertex3f(5.0, 0.0, 0.0);

		glEnd();
}

void simpleAxes (void) {
	glBegin (GL_LINES);
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f (-100.0, 0.0, 0.0);
		glVertex3f (100.0, 0.0, 0.0);

		glColor3f (0.0, 1.0, 0.0);
		glVertex3f (0.0, -100.0, 0.0);
		glVertex3f (0.0, 100.0, 0.0);

		glColor3f (0.0, 0.0, 1.0);
		glVertex3f (0.0, 0.0, -100.0);
		glVertex3f (0.0, 0.0, 100.0);
	glEnd ();
}

void MyDisplay (void) {
	cube box;
	pyramid tri;
	cuboid rect;
	shape *temp = p;
	while(temp)
	{
		temp -> draw();
		temp = temp -> getNext();
	}

	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	simpleAxes ();

//pyramid
	glPushMatrix();
	glRotatef(getRate(), 0, 1, 0);
	glTranslatef(-10.0, 0.0, 5.0);
	tri.render();
	glPopMatrix();

//cube
	glPushMatrix();
	glTranslatef(30.0, 10.0, 0.0);
	glRotatef(speed, 0, 1, 0);
	box.render ();
	speed ++;
	glPopMatrix();

//cuboid
	glPushMatrix();
	glTranslatef(-40.0, 10.0, 0.0);
	glRotatef(speed, 0, 1, 0);
	rect.render();
	speed++;
	glPopMatrix();

	glutSwapBuffers ();
	glutPostRedisplay ();
}

int main (int argc, char * argv[]) {
	shape shape;

	cube *b1 = new cube();
	cube *b2 = new cube();
	pyramid *p1 = new pyramid();
	cuboid *c1 = new cuboid();

	b1->setPosition(10,0,0);
	b1->setRate(1);

	b2->setPosition(10,-10,0);
	b2->setRate(3);

	p = b1;
	b1-> next = b2;
	b2-> next = p1;
	p1-> next = c1;
	c1-> next = NULL;

	glutInit (&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (800, 600);
	glutInitWindowPosition (100, 100);
	glutCreateWindow ("My First OpenGL Window");
	glutDisplayFunc (MyDisplay);
	glutReshapeFunc (rescale);		// to be called when window size has changed
	init ();
	glutMainLoop ();
	return 0;
}

This is my code for main_class.h

struct position
{
	float x, y, z;
} ;
struct rotation
{
	float a, x, y, z;
};

class shape
{
private:
	position pos;
	rotation orientation;
	float rate;
public:
	shape * next;
	void draw (void);
	void setNext(shape * n)
	{
		next = n;
	};
	shape* getNext(void)
	{
		return next;
	};
	void setPosition(float x ,float y ,float z)
	{
		pos.x = x;
		pos.y = y;
		pos.z = z;
	}

	void setRate(float r) 
	{
		rate = r;
	};
	int getRate(void)
	{
		return setRate();
	}
};

void shape::draw (void)
{
	glPushMatrix();
	glTranslatef (pos.x, pos.y, pos.z);
	glRotatef(orientation.a, orientation.x, orientation.y, orientation.z);
	void render(void);
	glPopMatrix();

	orientation.a += rate;
	if (orientation.a >= 360) 
	{	
		orientation.a -= 360;
	}
}

shape * p;

if i try to build this, the compiler gives me 2 errors saying

main_class.h(40) : error C2660: 'shape::setRate' : function does not take 0 arguments
main.cpp(188) : error C3861: 'getRate': identifier not found
Any help or tip would be greatly appreciated

This is your problem right here. There are a lot of problems with it:

void setRate(float r) 
{
  rate = r;
};
int getRate(void)
{
  return setRate();
}
  1. Inside getRate(), you are attempting to return the value returned by setRate(), which has no return value because it's specified as void.
  2. In order to do this, you must call setRate(). Your call has no arguments, but setRate() is specified to have a float argument.

Now that you know the error, I have a question for you.

Why on Earth would you call the setRate() method from the getRate() method to begin with!? That's completely contrary to the purpose of get and set methods!

I think you need to take another look at how you've set up your program's logic in general.

Edited 6 Years Ago by Fbody: n/a

ok i think i get what u mean. i think changing the return setRate() to rate would solve the problem and i tried it and it worked. Thx! :D

This article has been dead for over six months. Start a new discussion instead.