Use of Undeclared Identifiers

I'm trying to implement a database from a linked list of objects. For every variable in the main.cc class I keep getting the same error, eg. "use of undeclared identifier 'fname'; did you mean 'Passenger::fname'? - cin >> fname;". I understand what it means, but whatever I try I just keep coming up with errors.
Below is my whole program:

(database.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
#include<fstream>

using namespace std;

class Passenger {
public:
	Passenger(string, string, string);
	bool operator==(const Passenger&) const;
	bool operator<(const Passenger&) const;

	string fname, lname, destination;
	
};

class Flightlist {
public:
	int menu();
	void read_from_file(list<Passenger>&, string);
	void insert(list<Passenger>&, string, string, string);
	void remove(list<Passenger>&, string, string, string);
	bool check_reservation(list<Passenger>&, string, string, string);
	//void display_list(list<Passenger>&);
	//void save_to_file(list<Passenger>&, string)

	list<Passenger> flist;
};


(database.cc)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "database.h"

Passenger::Passenger(string first, string last, string dest)
{
 	fname = first;
 	lname = last;
 	destination = dest;
}

bool Passenger::operator==(const Passenger& p) const
{
	return fname == p.fname && lname == p.lname;
}

bool Passenger::operator<(const Passenger& p) const
{
	return fname < p.fname || (fname == p.fname && lname < p.lname);
}

int menu()
{
	int option;
	cout << endl;
	cout << "Enter one of the following options:" << endl;
	cout << "1. load reservations from file:" << endl;
	cout << "2. reserve a ticket" << endl;
	cout << "3. cancel a reservation" << endl;
	cout << "4. check reservation" << endl;
	cout << "5. display passenger list" << endl; 
	cout << "6. save passenger list" << endl;
	cout << "7. exit" << endl << endl;
	cin >> option;
	cin.get();
	return option;
}

void read_from_file(list<Passenger>& flist, string filename)
{
	string fname, lname, destination;
	ifstream input(filename.c_str());
	while (input >> fname >> lname >> destination) 
	{					
		flist.push_back(Passenger(fname, lname, destination));
	}
	input.close();
}

void insert(list<Passenger>& flist, string fname, string lname, string destination)
{
	flist.push_back(Passenger(fname, lname, destination));
}

void remove(list<Passenger>& flist, string fname, string lname, string destination)
{
	flist.remove(Passenger(fname, lname, destination));
}

bool check_reservation(list<Passenger>& flist, string fname, string lname, string destination)
{
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	return flist.end() != find(flist.begin(), flist.end(), Passenger(fname, lname, destination));
}

/*void display_list(list<Passenger>& flist)
{
	flist.sort();
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	for ( ; i1 != i2; ++i1) {
		cout << *i1 << endl;
	}
}

void save_to_file(list<Passenger>& flist, string filename)
{
	flist.sort();
	list<Passenger>::iterator i1, i2;
	i1 = flist.begin();
	i2 = flist.end();
	ofstream output(filename.c_str());
	for ( ; i1 != i2; ++i1) {
		output << *i1 << " ";
	}
	output.close();
}
*/


(main.cc)
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
65
66
67
68
69
70
71
72
73
74
75
#include "database.h"

int main()
{	
	Flightlist flight_list;

	while (true) 
	{
		switch (flight_list.menu())
		{
			case 1:	
				{
					flight_list.read_from_file(flight_list.flist, "ticket_reservations.dat");
					break;
				}

			case 2: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> Passenger::fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					insert(flight_list.flist, fname, lname, destination);
					break;
				}

			case 3: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					remove(flight_list.flist, fname, lname, destination);
					break;
				}

			case 4: 
				{
					cout << "first name of passenger:" << endl; 
					cin >> fname;
					cout << "last name of passenger" << endl;
					cin >> lname;
					cout << "destination of passenger" << endl;
					cin >> destination;
					if (check_reservation(flight_list.flist, fname, lname, destination))
						cout << "this passenger has a ticket reservation" << endl;
					else
						cout << "this passenger does not have a ticket reservation" << endl;
					break;
				}

			/*case 5:	
				{
					display_list(flight_list.flist);
					break;
				}

			case 6: 
				{
					save_to_file(flight_list.flist, "ticket_reservations.dat");
				}
				break;
			*/

			case 7:
				return 0;
		}
	}
	
	return 0;
}


Any tips would be incredibly appreciated, I just really need this code to compile.
Last edited on
If you want to use a variable named fname you must declare it before you can use it.

 
string fname;
Hi,

You're code here, is wrong (though, you're on the right track):

cin >> Passenger::fname;

A class by itself cannot and will not contain data. You need to declare an object based on the class and then store the data into it.

e.g.
1
2
3
Passenger firstPassenger;

cin>>firstPassenger.fname;


(Also, I don't recommend making your data members public as that violates pretty much the principle of OOP - make them private and give them get and set methods, instead.)

If you need any more help, feel free to email me. I also tutor C++ for a business (with extremely low rates), so if you need some constant help, let me know :)

Joe
sparkprogrammer@gmail.com
https://www.facebook.com/Concord-Spark-Tutoring-415848731922060/?ref=hl
Topic archived. No new replies allowed.