H-E-L-P!!!question about "list" )-:

Attached is the code I wrote, I used the library <list>.
I have got "red line" (error) under "print" function in the "main_01_05.cpp" file.
And I have no idea why?? ) -:
Waiting for your opinion ... Many thanks in advance
Harel

st.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
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef st
#define st

#include <string>
#include <iostream>
using namespace std;


class Stu

{


protected:

Stu( string , string , string , int );

//id
string id;

//first name
string f_n;  

//last name
string l_n;  


//number of courses
int num;

virtual bool milga();

virtual void print();

}

#endif 



BH.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BA_H
#define BA_H
#include "st.h"

class BA:public Stu

{

float score [10];

bool milga();

void print();

//constructor

BA( string , string , string , int , float []);

}
#endif 


MA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MA_H
#define MA_H
#include "st.h"

class MA:public Stu
{

public:
	
float score [10];
bool research;
bool milga();
void print();

//constructor

MA( string , string , string , int , float [] , bool);


}
#endif 



PHD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PHD_H
#define PHD_H
#include "st.h"

class PHD :public Stu
{
	
public:

//research hour
float res_hour;

bool milga();

void print();

//constructor
PHD( string , string , string , int , float);

}
#endif 


imp_st.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "st.h"

Stu::Stu( string id_ , string f_n_ , string l_n_ , int num_ )

{
	id=id_; 
	f_n=f_n_; 
	l_n=l_n_;
	num=num_;

};



imp_BA.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
#include "BA.h"

BA::BA( string id_ , string f_n_ , string l_n_ , int num_ , float score_ []):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert 10 score:" <<endl;
	for (int i=0 ; i<10 ; ++i)
	{
		cin>>score[i];
	}
};



bool BA::milga()
{

	float ave=0;
	for (int i=0;i<10;++i)
		ave+=score[i];
	ave/=10;
		if (num>=10 && ave>95 ) return 1;

};


void BA::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;
	}
};



imp_MA.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
#include "MA.h"

MA::MA( string id_ , string f_n_ , string l_n_ , int num_ , float score_ [] ,bool research_ ):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert 10 score:" <<endl;
	for (int i=0 ; i<10 ; ++i)
	{
		cin>>score[i];
	}

	cout<<"if the student do research then press 1 else press 0 : /t";
	cin>> research_;
};



bool MA::milga()
{

	float ave=0;
	for (int i=0;i<10;++i)
		ave+=score[i];
	ave/=10;
		if (num>=10 && ave>95 ) return 1;

};


void MA::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;

	}
};


imp_PHD.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
#include "PHD.h"

PHD::PHD( string id_ , string f_n_ , string l_n_ ,  int num_,  float res_hour_ ):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert number of research hour for this student: ";
	cin>> res_hour_ ;
};



bool PHD::milga()
{
		if (num>=2 && res_hour>25 ) return 1;

};


void PHD::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;

	}
};


main_01_05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "BA.h"
#include "MA.h"
#include "PHD.h"
#include <list>


int main()
{
	list <Stu*> all_stu;


	while ( all_stu.front() )
	{
		all_stu.front()->print();
		all_stu.pop_front();
	}

return 0;
}
Last edited on
There are no ()'s after print.
Stud doesn't have a public print function.

You probably want to put parenthesis after the function name to actually call the function.
all_stu.front()->print();

You should not call front() on an empty list. Change your loop condition to something like this instead
while ( !all_stu.empty() )

You have forgot to put semicolons after the class definitions.

You don't need a semicolon after the function definitions.
Last edited on
Sorry, I accidentally deleted it here. It is still with the red line ....
Hi peter , thank's.

"Stud doesn't have a public print function". -
You are right.
But I'll explain what I wanted to do.
I wanted that "Stu" will be my base class and all other classes inherit from it.
There are some functions I've defined as virtual (such as "print function").
So even though "print" is not defined in the base class as "public" is not supposed to do error.
Am I right?


" You have forgot to put semicolons after the class definitions"-

I did not understand what you mean, could you please send me the code segment which you speak?

"You don't need a semicolon after the function definitions"-

I did not understand what you mean, could you please send me the code segment which you speak?



thank about all , Harel
Last edited on
If you don't make print() public in Stu you are not allowed to call that function through a Stu pointer even if the function is public in the other classes. If you don't want to define the function in Stu you can make it a pure virtual function.
virtual void print() = 0;
This will forbid you to create instances of Stu and all subclasses that don't define the function. It is a good way to force subclasses to define certain functions.

1
2
3
4
5
6
7
8
9
class Foo
{
	void bar();
}; <-- put semicolon here!

void Foo::bar()
{

} <-- No need to put semicolon here!
Excellent! It works.

Just one last question:
If I want to move from one link to the next link in my List, but without deleting the link, what should I do?
Command that I used it (pop_front) delete the link in Front...
Last edited on
You can use iterators.
1
2
3
4
5
6
7
8
for (list<Stu*>::iterator it = all_stu.begin(); it != all_stu.end(); ++it)
{
	(*it)->print();
	/* The above will do the same as below. Use whatever you prefer.
	Stu* stu = *it;
	stu->print();
	*/
}


In C++11 you can also use range-based for loops to do the same thing.
1
2
3
4
for (Stu* stu : all_stu)
{
	stu->print();
}


If you are not familiar with iterators you probably should read up on it.
Last edited on
Hi,
I did it with the iterator exactly as you suggested.
But when I try to compiling the program I get this error:

>imp_st.obj : error LNK2001: unresolved external symbol "protected: virtual bool __thiscall Stu::milga(void)" (?milga@Stu@@MAE_NXZ)

What does it mean?

the fix code:


st.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
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef st
#define st

#include <string>
#include <iostream>
using namespace std;


class Stu

{


protected:

Stu( string , string , string , int );

//id
string id;

//first name
string f_n;  

//last name
string l_n;  


//number of courses
int num;

virtual bool milga();

public:
virtual void print()=0;

};

#endif 



BH.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BA_H
#define BA_H
#include "st.h"

class BA:public Stu

{

float score [10];

bool milga();

void print();

//constructor

BA( string , string , string , int , float []);

};
#endif 


MA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MA_H
#define MA_H
#include "st.h"

class MA:public Stu
{

public:
	
float score [10];
bool research;
bool milga();
void print();

//constructor

MA( string , string , string , int , float [] , bool);


};
#endif 



PHD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PHD_H
#define PHD_H
#include "st.h"

class PHD :public Stu
{
	
public:

//research hour
float res_hour;

bool milga();

void print();

//constructor
PHD( string , string , string , int , float);

};
#endif 


imp_st.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "st.h"

Stu::Stu( string id_ , string f_n_ , string l_n_ , int num_ )

{
	id=id_; 
	f_n=f_n_; 
	l_n=l_n_;
	num=num_;

}



imp_BA.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
#include "BA.h"

BA::BA( string id_ , string f_n_ , string l_n_ , int num_ , float score_ []):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert 10 score:" <<endl;
	for (int i=0 ; i<10 ; ++i)
	{
		cin>>score[i];
	}
}



bool BA::milga()
{

	float ave=0;
	for (int i=0;i<10;++i)
		ave+=score[i];
	ave/=10;
		if (num>=10 && ave>95 ) return 1;

}


void BA::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;
	}
}



imp_MA.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
#include "MA.h"

MA::MA( string id_ , string f_n_ , string l_n_ , int num_ , float score_ [] ,bool research_ ):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert 10 score:" <<endl;
	for (int i=0 ; i<10 ; ++i)
	{
		cin>>score[i];
	}

	cout<<"if the student do research then press 1 else press 0 : /t";
	cin>> research_;
}



bool MA::milga()
{

	float ave=0;
	for (int i=0;i<10;++i)
		ave+=score[i];
	ave/=10;
		if (num>=10 && ave>95 ) return 1;

}


void MA::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;

	}
}


imp_PHD.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
#include "PHD.h"

PHD::PHD( string id_ , string f_n_ , string l_n_ ,  int num_,  float res_hour_ ):Stu( id_ , f_n_ , l_n_ , num_) 
{
	
	cout<<"please insert number of research hour for this student: ";
	cin>> res_hour_ ;
}



bool PHD::milga()
{
		if (num>=2 && res_hour>25 ) return 1;

}


void PHD::print()
{
	if ( milga() ) 
	{
		cout << "student id: " ; cout<<id;
		cout << " student first name:" ; cout<<f_n;
		cout << " student last name:" ; cout<<l_n;

	}
}


main_01_05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "BA.h"
#include "MA.h"
#include "PHD.h"
#include <list>


int main()
{
	list <Stu*> all_stu;


	for ( list <Stu*> :: iterator it = all_stu.begin() ; it != all_stu.end() ; ++it)
	{
	(*it)->print();       // Stu* stu = *it;--->  stu->print();	
	}

return 0;
}
If you don't want to define milga() in Stu make it pure virtual as well.
Topic archived. No new replies allowed.