Nested Classes. a colon right after the Class declaration

I am a beginner in C++ so excuse me if this is a stupid question.Note I read all the tutorials on this site. Anyways here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;
#include "matplotpp.h"
class M1 :public MatPlot{ 
void DISPLAY(){
    vector<double> x(100),y(100);    
    for(int i=0;i<100;++i){
	x[i]=0.1*i;
	y[i]=cos(x[i]);
    }
    plot(x,y);
}
}mp;

void display(){ mp.display(); }
void reshape(int w,int h){ mp.reshape(w,h); }
int main(int argc,char* argv[]){
    glutInit(&argc, argv);
    glutCreateWindow(100,100,400,300);
    glutDisplayFunc( display);
    glutReshapeFunc( reshape );
    glutMainLoop();    
    return 0;
}

This is compiling fine and producing the desired result, i.e. plots the sin(x) curve. What I want to understand is this piece of code: "class M1 :public MatPlot{..." As I see it you are defining a new class M1 with the already defined class MatPlot as a public member in the class. If that is correct, is there a way where I can define class M1 in a conventional way like
1
2
3
4
5
6
class M1 {
public:
int z;
 MatPlot{ ...

}mp;


PS: . My end objective is to use an array of doubles I have and use this code to pplot that array.
Last edited on
As I see it you are defining a new class M1 with the already defined class MatPlot as a public member in the class.
Incorrect. You are defining a new class M1 which inherits from the already defined class MatPlot. What does the class MatPlot look like?
It's a huge header file. But I am copying the relevant declarations. let me know if its insufficient.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MatPlot{
...
public:
    
    MatPlot();
    ~MatPlot();

    void virtual DISPLAY(){};

    void inline debug1(){is_debug1=1;}
    void inline debug2(){is_debug2=1;}

    // GLUT Callback Functions ///
    void display();
Last edited on
Thanks to booradley I understand it better now. I should've read the inheritances before. Apologies.
i understood the code and made changes so that I can pass an array of doubles and plot that rather than some pre-defined function sin(x).
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
using namespace std;
#include "matplotpp.h"
class MP :public MatPlot{
public:
	double* z;
	void DISPLAY(){
		vector<double> x(100), y(100);
		for (int i = 0; i<100; ++i){
			x[i] = 0.1*i;
			y[i] = z[i];
		}
		plot(x, y);
	}

}mp;
void display1(double* y){ 
	mp.z = y; mp.display(); }
void reshape(int w, int h){ mp.reshape(w, h); }
int main(int argc,char* argv[]){
	double y[100];
	for (int i = 0; i < 100; ++i){
		y[i] = tan(i);
	}
    glutInit(&argc, argv);
    glutCreateWindow(100,100,400,300);
    glutDisplayFunc( display1(y));
    glutReshapeFunc( reshape );
    glutMainLoop();    
    return 0;
}


This throws a compile error "IntelliSense: argument of type "void" is incompatible with parameter of type "void (*)()". What does this mean? Note that this compilation error comes from the line: "glutDisplayFunc( display1(y));"


Last edited on
The function glutDisplayFunc is probably expecting to be passed a pointer to a function as a parameter. Look at glutReshapeFunc on the next line. You're passing the name of the function (which acts as a pointer to the function itself). So the line the compiler is complaining about should probably look like this:
glutDisplayFunc( display1 );
glutDisplayFunc expects a pointer to a function. On line 26 you aren't giving it a pointer. You're giving it the result of calling display1 with argument y. The return type of display1 is void. Thus the error message.

Notice that the type of the pointer expected is void (*)() which is a pointer to a function that takes no parameters. You cannot feed it the address of display1 without the compiler barfing. You'll need to take a different approach.
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
using namespace std;
#include "matplotpp.h"
class MPlot :public MatPlot{
public:
	double* z;
	int zLength;
	void DISPLAY(){
		cout << "Entering Display " << endl;
		vector<double> x, y;
		x.resize(zLength);
		y.resize(zLength);
		for (int i = 0; i<zLength; ++i){
			x[i] = 0.00096*i;
			y[i] = z[i];
		}
		plot(x, y);
	}

};
MPlot mp;
void display(){ mp.display(); }
void reshape(int w, int h){ mp.reshape(w, h); }

void plot2D(int a, char *b[], double* y, int yLength){
	mp.z = y;
	mp.zLength = yLength;
	getchar();
	glutInit(&a, b);//Ignore these a and b arguments. The original code passes the inputs to
                              // the main (int argc and char* argv) to the glutInit.
	glutCreateWindow(100, 100, 400, 300);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
}

This is how I finally did it and it works. Thanks guys for your help.
Topic archived. No new replies allowed.