Need help with class implementations

I'm trying to write a student class registration program that uses three classes: class Course, class Student, and class Wrapper(to eliminate the need for global declarations). I've written them out, but I'm getting all sorts of errors. I have no idea how to fix them. :(

Errors:

homework3.h:29 multiple types in one declaration
homework3.h:45 expected primary-expression before '.' token
At global scope:
homework3.h:55 multiple types in one declaration
homework3.h:155 'class Course' has no member named 'getname'
homework3.h:184 'class Course' has no member named 'getname'
homework3.h:185 'class Course' has no member named 'getprofessor'


Header file:
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef WRAPPER_H
#define WRAPPER_H
#include <iostream>
#include <list>
using namespace std;

class Course

class Student
{
    typedef list<Course *>::iterator citerator;
    
    public:
        Student (string n): nameText(n) { }
        string getWaitList() {return waitlist;}
        //operations
        string name() {return nameText;}
        void addCourse(Course * c) {courses.push_back(c);}
        citerator firstCourse() {return courses.begin();}
        citerator lastCourse() {return courses.end();}
        void removeCourse (citerator & citer) {courses.erase(citer);}
        
    private:
        string waitlist;
        string nameText;
        list <Course * > courses;
        
        //friend class Wrapper
};


class Student

class Course
{
    friend class Wrapper;
    
    public:
        Course(string n, int s, string p, string c):name(n), max(s), 
            professor(p), classroom(c) { }
        Course(string n, int s):name(n), max(s) { }
        string getName() {return name;}
        string getProfessor() {return professor;}
        string getClassroom() {return classroom;}
        bool full() {return Student.size()>=max;}
        void addStudent (Student * s) {students.push_back(s);}
        void generateClassList();
        
    private:
        string professor;
        string classroom;
        string name;
        int max;
        list <Student *> students;
};

class Wrapper
{
	public:
		Wrapper();
		Wrapper(list <Course *> course, list <Student *> student);
		void readStudents(istream & infile);
		void fillCourses();
		Student * findStudent (string & searchName);
		Course * findCourse (string & searchName);
		string getwname() { return wname; };
		void readCourses(istream & infile);
		void generateCourseReports();
		void generateStudentReports();
	protected:
		string wname;
		string professor;
		string classroom;
		list <Student *> all_students;
		list <Course *> all_courses;
};
Wrapper::Wrapper()
{
	all_students;
	all_courses;
}
void Wrapper::readCourses(istream & infile)
{
	string name;
	int max;
	string professor;
	string classroom;
	while (infile >> name >> max >> professor >> classroom)
	{
		Course * newCourse = new Course (name, max, professor, classroom);
		all_courses.push_back(newCourse);	
	}
}
void Wrapper::readStudents(istream & infile)
{
	string name;
	string course;
	while (infile >> name >> course)
	{
		Student * theStudent = findStudent(name);
		Course * theCourse = findCourse(course);	
		
		if (theCourse != 0)
		{
			theStudent -> addCourse(theCourse);
		}
		else
			cout << "student " << name << " requested invalid course " <<
                course << endl;
	}
}
void Wrapper::fillCourses()
{
	list<Student *>::iterator s_start, s_end;
	s_start = all_students.begin();
	s_end = all_students.end();
	
	for ( ; s_start != s_end; ++s_start)
	{
		list<Course *>::iterator c_start, c_end, c_next;
		c_start = (*s_start) -> firstCourse();
		c_end = (*s_start) -> lastCourse();
		for ( ; c_start != c_end; c_start = c_next)
		{
			c_next = c_start; ++c_next;
			if (!(*c_start) -> full())
				(*c_start) -> addStudent(*s_start);
			else
				(*s_start) -> removeCourse(c_start);
		}
	}
}
Student * Wrapper::findStudent (string & searchName)
{
	list <Student *>::iterator start, stop;
	start = all_students.begin();
	stop = all_students.end();
	for ( ; start != stop; ++start)
	{
		if ((*start) -> name() == searchName)
			return *start;
	}
		//if not found, make one
	Student * newStudent = new Student(searchName);
	all_students.push_back(newStudent);
	return newStudent;
}
Course * Wrapper::findCourse (string & searchName)
{
	list <Course *>::iterator start, stop;
	start = all_courses.begin();
	stop = all_courses.end();
	for ( ; start != stop; ++start)
	{
		if ((*start) -> getname() == searchName)
			return *start;
	}
		//if not found, make one
	Course * newCourse = new Course(searchName, NULL);
	return newCourse;
}
void Wrapper::generateCourseReports()
{	
	list <Course *>::iterator start, stop;
        start = all_courses.begin();
        stop = all_courses.end();
        for ( ; start != stop; ++start)
                (*start) -> generateClassList();
}
void Wrapper::generateStudentReports()
{
	list <Student *>::iterator s_start, s_stop;
	list <Course *>::iterator c_start, c_stop;
	s_start = all_students.begin();
	s_stop = all_students.end();
	
	for ( ; s_start != s_stop; ++s_start)
	{
		cout << endl;
		cout << "Class list for " << (*s_start)->name() << ":" << endl << endl;
		c_start = (*s_start) -> firstCourse();
		c_stop = (*s_start) -> lastCourse();
		for ( ; c_start != c_stop; ++c_start)
			cout << (*c_start) -> getname() << " with professor " << 
                (*c_start) -> getprofessor() << endl;
	}
}

#endif 


.cpp file.
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 "homework3.h"
#include <iostream>
#include <list>
//#include "Wrapper.h"
//#include "Student.h"
//#include "Course.h"
using namespace std;

bool studentCompare(Student * a, Student * b)
{
    return a->name()>b->name();
}

void Course::generateClassList()
{
    students.sort(studentCompare);
    cout<<endl<<"Class list for "<<getName()<<" with Professor "<<
        getProfessor()<<" in class "<<getClassroom()<<":"<<endl;
    list<Student *>::iterator start, stop;
    start=students.begin();
    stop=students.end();
    for( ; start!=stop;++start)
    {
        cout<<(*start)->name()<<endl;
    }
}
Line 7: Missing semicolon

Why are you using std::list instead of std::vector? It's horribly inefficient in most cases (such as this case).

Line 32: Pointless forward declaration, also missing semicolon.

Also, why are you using pointers for everything?

Your main problem is that you've got full definitions in your header file - either define them inline (inside the class defintion) or move them to the .cpp source file. Full function definitions don't belong in headers unless they're trivial.
Last edited on
Thanks. I got rid of most of the problems. I split the classes into separate files and put all the member functions into the .cpp file.

The professor wants us to use list. And the assignment was to take a program from the book and improve it, so the abundance of pointers wasn't my idea. :P

Now there's just one error:

Course.h:22 expected primary-expression before '.' token


for this line:

 
bool full() {return Student.size()>=max;}
Did you mean bool full() { return students.size() >= max; }?
Yeah, I did mean that. XD

Now I'm getting a linker error!

In function `main'::
[Linker Error] undefined reference to `WinMain@16'
ld returned 1 exit status


There's also this warning:

In member function 'Course* Wrapper::findCourse(std::string&)':
91:50 [Warning] passing NULL to non-pointer argument 2 of 'Course::Course(std::string, int)' [-Wconversion-null]


It doesn't seem to like the NULL is this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Course * Wrapper::findCourse (string & searchName)
{
	list <Course *>::iterator start, stop;
	start = all_courses.begin();
	stop = all_courses.end();
	for ( ; start != stop; ++start)
	{
		if ((*start) -> getName() == searchName)
			return *start;
	}
		//if not found, make one
	Course * newCourse = new Course(searchName, NULL);
	return newCourse;
}


Help?
About the first message (the linker error): do you have a main function?

For the second (the warning): you're passing NULL (which is typically used for pointers) to an int parameter (representing the max size of the class, it seems).
What does it mean for the max size to be "null"?
._. Now it compiles.

I hate asking, but can I see some example code for how to apply this data to the main function? I'm completely clueless when it comes to the fstream library.

Course.txt
1
2
3
4
ART101 4 Ted UH200
HIST213 3 Joan CS110
MATH412 2 Jackie JBH389
CSCI330 3 Joe JBH146


Student.txt
1
2
3
4
5
6
7
8
9
10
11
12
Smith,Amy ART101
Smith,Amy MATH412
Jones,Randy HIST213
Cobb,Kevin MATH412
Mo,Jo CSCI330
Smith,Kathy CSCI330
Jones,Randy CSCI330
Smith,Kathy MATH412
Smith,Kathy ART101
Cobb,Kevin CSCI330
No,No CSCI330
No,No MATH412
Topic archived. No new replies allowed.