Function that returns list of objects

Hi all, I am a new member here and I am glad to be a part of this great community :)

I want to discuss with you some problems that I have with my course project.

We have:
1. Class CPlane with brand and flight hours.
2. Class CAirtravel with name of company, nationality and map <CPlane, unsigned> for planes and number of destinations.

I have to write a member function of class CAirtravel that returns list<CPlane> of planes with flight hours < 2000 and number of destinations > 10.

Here is part of 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
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
64
class CPlane {
public:
	string brand;
	unsigned hours;

	CPlane() {
	    brand = ""; hours = 0;  }

	CPlane(string x, unsigned y) {
		brand = x; hours = y;  }
};

    bool operator<(const CPlane &ob1, const CPlane &ob2) {
		return ob1.brand < ob2.brand;  }

class CAirtravel { 
	string company, nation;
	map <CPlane, unsigned> plane;
public:
      CAirtravel() {
	        unsigned hr, n_dest, num;
		string br, dest;
		system("cls");
		cout<<"\nEnter name of the company: "; cin>>company;
		cout<<"\nEnter nationality: "; cin>>nation;
		cout<<"\nEnter number of planes: "; cin>>num;

		for(int i = 0; i < num; i++) {
			system("cls");
			cout<<"\nEnter plane's brand: "; cin>>br;
			cout<<"\nEnter flight hours: "; cin>>hr;
	cout<<"\nEnter number of flight destinations: ";  cin>>n_dest;
			plane.insert( make_pair(CPlane(br,hr), n_dest) ); 	
		}

   list returnLst() {   //Here start the problems
	map <CPlane, unsigned>::iterator itr;
	static list <CPlane> lst;

	for(itr = plane.begin(); itr != plane.end(); itr++)			
	  if((itr->first.hours<2000)&&(itr->second>10))
              lst.push_back(itr->first);

	return lst; }
};

   void main() {
		vector <CAirtravel> v;
		list <CPlane> plane;
		unsigned c_n;

		cout<<"\nEnter number of companies: "; cin>>c_n;

		for(int i = 0; i < c_n; i++) 		
			v.push_back(CAirtravel());

                for(i=0; i<v.size(); i++) {
				if(i == v.size() - 1) { 			
					plane = v[i].returnLst(); break; //1.
				}
				v[i].returnLst(); 
		}
/* 1. The compiler shows error: "binary '=' : no operator defined which takes a right-hand operand of type 'class std::list' (or there is no acceptable conversion)" */
}

I cant change the conditions, it must be done with a member function of CAirtavel that returns list<CPlane>.

I tried to predefine operator "=" after definition of class CPlane:
1
2
3
4
	CPlane operator=(const CPlane &x) {
		brand = x.brand;
		hours = x.hours; 	
                return *this; }


Still it doesn't work. I am doing something wrong and I hope you guys can help me ;)

Last edited on
I think the function returnList() should return a list <CPlane> --- BUT --- you are returning standard list so there is a type mismatch
Last edited on
by the way - any reason why the for loop code to get the plane info is written like this? It looks wrong/odd.

1
2
3
4
5
6
7
8
 
                for(i=0; i<v.size(); i++) {
				if(i == v.size() - 1) { 			
					plane = v[i].returnLst(); break; //1.
				}
				v[i].returnLst(); 
		}
 


I didn't notice the type mismatch at all, many thanks :)

So the right syntax is:
list <CPlane> returnList()

The loop code looks a little strange, but since there are many CAirtravel objects in the vector, I need to call returnLst() for every object in order to accumulate the static list of planes.
In the last cycle I get the full list, containing all planes that meet the conditions.
Maybe it isn't an effective approach, I will be happy if you can suggest some code optimizations :)
Last edited on
Hi, I am in a difficult situation again.
To make things more clear, constructors and most of the functions are omited:
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
class CPlane {
public:
       string brand;       //I need to print this variables using 
       unsigned hours;  //multimap <CAirtravel, unsigned> airtravel
                                   //in class CAirport	
};

class CAirtravel { 
public:
	string company, nation;
	map <CPlane, unsigned> plane; //!
	multimap <string, unsigned> destination; 

void print() {
		map <CPlane, unsigned>::iterator iter;
		multimap <string, unsigned>::iterator iter2;
		cout<<"\nName of the company: "<<company;
		cout<<"\nNationality: "<<nation;		

		for(iter = plane.begin(); iter != plane.end(); iter++) {
			cout<<"\n\nPlane's brand: "<<iter->first.brand;	
			cout<<"\nFlight hours: "<<iter->first.hours;
			cout<<"\nFlight destinations: "<<iter->second; 
		}

      for(iter2 = destination.begin(); iter2 != destination.end(); iter2++) {
			cout<<"\n\nDestination: "<<iter2->first;
			cout<<"\nFlights: "<<iter2->second; 
		}		
	}
};

class CAirport {
	string name;
	unsigned flight;
	multimap <CAirtravel, unsigned> airtravel; 
};


I need to write data output function member of CAirport, but I can't get out the contents of the maps in CAirtravel objects through multimap <CAirtravel, unsigned> airtravel:
1
2
3
4
5
6
7
8
9
10
11
12
13
	void info() {
		multimap <CAirtravel, unsigned>::iterator m;	

		cout<<"\nName of airport: "<<name;
		cout<<"\n\nFlight number: "<<flight;
		cout<<"\n\nData of companies:\n\n";

		for(m = airtravel.begin(); m != airtravel.end(); m++) {
			cout<<"\nCompany: "<<m->first.company;
                                    cout<<"\nNationality: "<<m->first.nation;
			cout<<"\nNumber of flights: "<<m->second; 
//cout<<m->first.plane.size() works but i want to print the contents too.
		}

I tried to call the CAirtravel member function print():
m = airtravel.begin(); m->first.print();
But the result is error:
" 'print' : cannot convert 'this' pointer from 'const class CAirtravel' to 'class CAirtravel &' "
Last edited on
Here is a working variant of info() function:
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
void info() {
 
		multimap <CAirtravel, unsigned>::iterator iair = airtravel.begin();		
 
		cout<<"Airport: "<<name;
		cout<<"nFlight number: "<<flight;
		cout<<"nnData of companies:";
 
		for(;  iair != airtravel.end();  iair++) {
			cout<<"nnCompany: "<<iair->first.company;
			cout<<"nNationality: "<<iair->first.nation;
 
			map <CPlane, unsigned> pl(iair->first.plane);
			map <CPlane, unsigned>::iterator ipl = pl.begin();
 
			cout<<"nnList of company's planes:";
 
			for(;ipl != pl.end(); ipl++) {
				cout<<"nBrand: "<<ipl->first.brand;
				cout<<"nFlight hours: "<<ipl->first.hours;
				cout<<"nDestinations: "<<ipl->second;
			}
 
                                    multimap <string, unsigned> ds(iair->first.destination);
			multimap <string, unsigned>::iterator ids = ds.begin();
 
			cout<<"nnList of company's destinations:";
 
			for(;ids != ds.end(); ids++) {
				cout<<"nnName of destination: "<<ids->first;
				cout<<"nFlights to "<<ids->first<<": "<<ids->second; 
			}
 
	        cout<<"nnNumber of all company's flights: "<<iair->second<<endl<<endl;
			system("pause"); system("cls"); 
		}		
	}


10x again guestgulkan :)
Topic archived. No new replies allowed.