Calling variables from other functions

Hey guys something I'm not sure how to do is to use a variable defined in a function in an entirely separate function. In this case I'm trying to get the length and width of something I'd defined in the function called drawBody. I need the same length and width to define where the wheel should be drawn in a separate function called drawWheel. Any suggestions? Very new to C++ and especially Gl
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
void Vehicle::drawBody(double length,double height,double width) {

	glBegin(GL_QUADS);
	// front face
	glColor3f(0,1,0);
	glVertex3f(length, 0, 0);
	glColor3f(1,0,0);
	glVertex3f(length, height, 0);
	glColor3f(0,0,1);
	glVertex3f(length, height, width);
	glColor3f(0,1,1);
	glVertex3f(length, 0, width);
	// back face
	glVertex3f(0, 0, 0);
	glVertex3f(0, height, 0);
	glVertex3f(0, height, width);
	glVertex3f(0, 0, width);
	// right face
	glColor3f(1,0,0); //Red
	glVertex3f(0, 0, width);
	glVertex3f(0, height, width);
	glVertex3f(length, height, width);
	glVertex3f(length, 0, width);
	// left face
	glColor3d(1,1,1);
	glVertex3f(0, 0, 0);
	glVertex3f(0, height, 0);
	glVertex3f(length, height, 0);
	glVertex3f(length, 0, 0);
	// Top face
	glColor3f(0,0,1);
	glVertex3f(0, height, 0);
	glVertex3f(0, height, width);
	glVertex3f(length, height, width);
	glVertex3f(length, height, 0);
	// Bottom face
	glVertex3f(0, 0, 0);
	glVertex3f(0, 0, width);
	glVertex3f(length, 0, width);
	glVertex3f(length, 0 , 0);
	glEnd();
};

void Vehicle::drawWheel(double radius, double whlwid) {
	GLUquadricObj *qobj;
	qobj = gluNewQuadric();
	glPushMatrix;
	glTranslated(length,0,width);
	glRotated(90,0,0,1);
	glColor3f(0,1,0);
	glBegin(GL_POLYGON);
	for(float angle = 0; angle < 360; angle++) {
		double x = (cos(angle) * radius);
		double y = (sin(angle) * radius); 
		glVertex2f(x, y);	
	}	
	glEnd();
	gluCylinder(qobj, radius, radius, whlwid, 8, 1);
	glPopMatrix;
};
You can make the length and width member variables of the Vehicle class.
But then I would need to reenter the data a second time! Is there a way I could just call the variables all ready entered into drawBody?
closed account (D80DSL3A)
Add length and width as arguments to the drawWheel() function?
void Vehicle::drawWheel(double radius, double whlwid, double length, double width)
But then I would need to reenter the data a second time! Is there a way I could just call the variables all ready entered into drawBody?

No you wouldn't. Say length is a member variable of Vehicle. If that length is set in a call to drawBody, then if drawWheel is called after, then length would still be what it was just after the call to drawBody.

Also, I notice that your methods aren't even touching any member variables. I wouldn't think that such methods would be static methods (as they seem to be connected with an instance of Vehicle). I would recommend that the body length, width, and height, as well as the wheel radius and width should be member variables of Vehicle.

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
//Vehicle.h
#pragma once

#ifndef _VEHICLE_H
#define _VEHICLE_H

class Vehicle
{
     private:
          double bodyLength;
          double bodyWidth;
          double bodyHeight;
          double wheelRadius;
          double wheelWidth;

     public:
          Vehicle(double bl, double bw, double bh, double wr, double ww)
          {
               bodyLength = bl;
               bodyWidth = bw;
               bodyHeight = bh;
               wheelRadius = wr;
               wheelWidth = ww;
          }

          void drawBody();
          void drawWheel();
};

#endif 


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
62
63
//Vehicle.cpp
#include "Vehicle.h"

void Vehicle::drawBody() {

	glBegin(GL_QUADS);
	// front face
	glColor3f(0,1,0);
	glVertex3f(bodyLength, 0, 0);
	glColor3f(1,0,0);
	glVertex3f(bodyLength, bodyHeight, 0);
	glColor3f(0,0,1);
	glVertex3f(bodyLength, bodyHeight, bodyWidth);
	glColor3f(0,1,1);
	glVertex3f(bodyLength, 0, bodyWidth);
	// back face
	glVertex3f(0, 0, 0);
	glVertex3f(0, bodyHeight, 0);
	glVertex3f(0, bodyHeight, bodyWidth);
	glVertex3f(0, 0, bodyWidth);
	// right face
	glColor3f(1,0,0); //Red
	glVertex3f(0, 0, bodyWidth);
	glVertex3f(0, bodyHeight, bodyWidth);
	glVertex3f(bodyLength, bodyHeight, bodyWidth);
	glVertex3f(bodyLength, 0, bodyWidth);
	// left face
	glColor3d(1,1,1);
	glVertex3f(0, 0, 0);
	glVertex3f(0, bodyHeight, 0);
	glVertex3f(bodyLength, bodyHeight, 0);
	glVertex3f(bodyLength, 0, 0);
	// Top face
	glColor3f(0,0,1);
	glVertex3f(0, bodyHeight, 0);
	glVertex3f(0, bodyHeight, bodyWidth);
	glVertex3f(bodyLength, bodyHeight, bodyWidth);
	glVertex3f(bodyLength, bodyHeight, 0);
	// Bottom face
	glVertex3f(0, 0, 0);
	glVertex3f(0, 0, bodyWidth);
	glVertex3f(bodyLength, 0, bodyWidth);
	glVertex3f(bodyLength, 0 , 0);
	glEnd();
}

void Vehicle::drawWheel() {
	GLUquadricObj *qobj;
	qobj = gluNewQuadric();
	glPushMatrix;
	glTranslated(bodyLength,0,bodyWidth);
	glRotated(90,0,0,1);
	glColor3f(0,1,0);
	glBegin(GL_POLYGON);
	for(float angle = 0; angle < 360; angle++) {
		double x = (cos(angle) * wheelRadius);
		double y = (sin(angle) * wheelRadius); 
		glVertex2f(x, y);	
	}	
	glEnd();
	gluCylinder(qobj, wheelRadius, wheelRadius, wheelWidth, 8, 1);
	glPopMatrix;
}


1
2
3
4
5
//Some Other.cpp

Vehicle vehicleInstance(6, 1.5, 2.0, 0.5, 0.2);
vehicleInstance.drawBody();
vehicleInstance.drawWheel();
Last edited on
'void Vehicle::drawWheel(double,double,double,double)' : overloaded member function not found in 'Vehicle'

It overloads it :/
That means that there's no matching function prototype in the class definition.

1
2
3
4
5
6
7
class Vehicle
{
     //...

     //need to add this
     void drawWheel(double radius, double whlwid, double length, double width);
};
Topic archived. No new replies allowed.