Expression Must have Class Type

Okay, I have a class that reads in model coordinates, indices and colours from a text file.
after that was finished I created a Cube class so that I could draw multiple copies of my cube, now here comes the problem. Within the cube class I created a pointer to a model class so that I didn't have to load the model for every cube.

Model *model;

The problem comes whenever I try to create a new object from my Cube class and it says "expression must have class type". Here is the code :

objects constructed at the beginning of my code :

1
2
Cube cube();
Model cubeModel("Model1.txt");


In my Initialise method I try to point to the model object:

cube.model = &cubeModel;

In my draw I try to draw the model to screen:

cube.model->draw();


Any help would be much appreciated !
Many Thanks

Edit: Cube.h and Cube.cpp

Cube.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Windows.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include <sstream>
#include <iostream>
#include <stdio.h>

#ifndef CUBE_H
#define CUBE_H
#include "Model.h"
#include "Vector3.h"
using namespace std;

class Cube {
private:

	Vector3 position;
public:

	Model *model;
	Cube::Cube();
	void Cube::Update();
};
#endif 


Cube.cpp

1
2
3
4
5
6
7
8
9
#include "Cube.h"

Cube::Cube()
{
}

void Cube::Update()
{
}




Last edited on
First thing I'd point out, at lines 22 and 23, the constructor and the Update function are over-qualified. Don't use :: operator when you're inside the class.

Does that solve your problem?
What's the line where you get the error?
No, It didn't fix it but it did throw me a couple of extra errors as well.

as well as the Class type error I'm getting,

left of '.model' must have class/struct/union.

cube.model->draw();

and here

cube.model = &cubeModel;

so the same lines as before..

I'm still unsure as to why it's happening xD
Last edited on
Your class should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
using namespace std;

class Cube {
private:

	Vector3 position;
public:

	Model *model;
	Cube();
	void Update();
};


are you doing that? please show me the full code with which you create and call the class.
If you're defining your class in a header, please do not put using namespace std; at the top. It pollutes the namespace of anything including that header, without warning.
Last edited on
Cube.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Windows.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include <stdio.h>

#ifndef CUBE_H
#define CUBE_H
#include "Model.h"
#include "Vector3.h"

class Cube {
private:

	Vector3 position;
public:

	Model *model;
	Cube();
	void Update();
};
#endif 


Cube.cpp

1
2
3
4
5
6
7
8
9
#include "Cube.h"

Cube::Cube()
{
}

void Cube::Update()
{
}


Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "main.h"


camera cam = {3, 2, 5, 0, 0, 0, 0, 1, 0};
Cube cube();
Model cubeModel("Model1.txt");
float angle = 0.1;
int speed = 2;

void display() {

	glClear(GL_COLOR_BUFFER_BIT );//| GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); // reset the matrix
	gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z, cam.lookAt.x, 
		cam.lookAt.y, cam.lookAt.z, 
		cam.up.x, cam.up.y, cam.up.z);

	glPushMatrix();
	glRotatef(angle * speed, 0.0f, 1.0f, 0.0f);
	cube.model->draw();
	glPopMatrix();
	glPopMatrix();
	::glTranslatef(2,0,1);
	

	
	glutSwapBuffers();
	speed++;

	
}

void init() {
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glColor3f(0.0, 0.0, 0.0);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	cube.model = &cubeModel;
}

void reshape(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 10.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitWindowSize(500, 500);
	glutInitDisplayMode(GLUT_DOUBLE);
	glutCreateWindow("Cube");
	glutDisplayFunc(display);
	glutIdleFunc(display);
	glutReshapeFunc(reshape);
	init();
	glutMainLoop();
}


Here is my Cube class and main files
So!

First thing, I would like to inform you, that OpenGL is a function based library. Using classes to handle it is a bad idea. I have made 2 simulations with OpenGL and I never needed to handle similar objects with other than global functions.

Second thing, why are you defining those variables cam, cube, cubemodel, ... in the global scope? this is very not safe and very not efficient for your program. It doesn't make sense at all that you want to use object oriented nature for the function oriented OpenGL, while you use global variables like this... what's the use of your class if you have all those variables thrown here and there?

Your program compiled (after I removed all OpenGL stuff), and since you didn't give me the other files, I just used some typedefs to define them. And the most terrible mistake was instantiating an object from Cube with brackets () while the constructor is not used. That caused an error. Here's what worked with me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "cube.h"

int cam[] = {3, 2, 5, 0, 0, 0, 0, 1, 0};
Cube cube;
Model cubeModel;
float angle = 0.1;
int speed = 2;

void display() {



}

void init() {

    cube.model = &cubeModel;
}

void reshape(int w, int h) {

}
int main(int argc, char** argv) {

    init();
}


1
2
3
4
5
6
7
8
9
#include "cube.h"

Cube::Cube()
{
}

void Cube::Update()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Windows.h"
#include <GL/gl.h>
#include <GL/glu.h>
//#include "glut.h"
#include <stdio.h>

#ifndef CUBE_H
#define CUBE_H
//#include "Model.h"
//#include "Vector3.h"

typedef double Vector3;
typedef double Model;

class Cube {
private:

    Vector3 position;
public:

    Model *model;
    Cube();
    void Update();
};
#endif 


If you still get an error, tell me where the error occures exactly.
Last edited on
Hey Destroyer,
The reason I am using an OO approach is that I have to draw more than one and control more than one of the cubes. An OO approach seemed like the logical option given my needs.

You'll be glad to hear that getting rid of the parenthesis when I called the cube constructor got rid of my errors and fixed the program.

I swear it's always the little things XD

Thankyou all for you help

Your welcome body :)

But I'm gonna tell you something. It doesn't make sense with OpenGL to use OO approach because you're gonna redraw your cube (or object in general) every few milliseconds according to different calculation scheme. So you're gonna have to call the plot function with different parameters (which can't be modeled realistically with OO approach). That's why there's no need to have a class to hold your OpenGL objects. That's my opinion.

Cheers.
Topic archived. No new replies allowed.