(This programm includes SafeArray.h course.h main.cpp) Thanks !!
#ifndef SAFEARRAY_H
#define SAFEARRAY_H
---------------------------------------------------------------------------------
using namespace std;
template <class T>
class SafeArray {
protected:
T *storage;
int count;
public:
SafeArray();
SafeArray(int);
virtual ~SafeArray(void);
virtual T& operator[] (int) throw(out_of_range);
};
// default constructor
template <class T>
SafeArray<T>::SafeArray () : storage(NULL), count(0) {}
// single param constructor
template <class T>
SafeArray<T>::SafeArray (int count) : storage(new T[count]), count(count) {}
// destructor
template <class T>
SafeArray<T>::~SafeArray(void) {
delete [] storage;
}
// overloaded [] operator
template <class T>
T& SafeArray<T>::operator[] (int index) throw (out_of_range) {
if (index < 0) {
throw out_of_range("Index is below 0");
}
if (index >= count) {
throw out_of_range("Index is too high");
}
return storage[index];
}
#endif
-------------------------------------
#ifndef COURSE_H
#define COURSE_H
#include<cstdlib>
#include <string>
#include"safearray.h"
using namespace std;
int const MAX_LINES = 10;
class course {
public:
string name;
string title;
safearray<string> description(MAX_LINES);
course() : name(""), title("") {}
course(string name, string title) :
name(name), title(title) {}
friend ostream& operator<<(ostream&, const course&);
friend istream& operator>>(istream&, course&);
};
ostream& operator<<(ostream& out, const course& c) {
out << c.name << ": " << c.title << "\n";
int index = 0;
while (c.description[index] != "") {
out << c.description[index++] << "\n";
}
return out;
}
istream& operator>>(istream& in, course& c) {
getline(in, c.name);
getline(in, c.title);
string line;
getline(in, line);
int number = 0;
while (line != "") {
c.description[number++] = line;
getline(in, line);
}
return in;
}
#endif
------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#include "course.h"
void init_courses(course[]);
void display_menu(course[]);
const int NUM_COURSES = 10;
const int QUIT = 99;
int main(int argc, char* argv[]) {
course courses[NUM_COURSES];
init_courses(courses);
int choice = 0;
while (choice != QUIT) {
display_menu(courses);
cout << "Enter number of course to see more information on\n";
cin >> choice;
cout << "\n";
if (choice >= 1 && choice <= NUM_COURSES) {
cout << courses[choice - 1] << "\n\n";
}
}
return EXIT_SUCCESS;
}
void display_menu(course courses[]) {
for (int i = 1; i <= NUM_COURSES; i++) {
cout << i << ". " << courses[i - 1].name << "\n";
}
cout << "99. Quit\n";
}
void init_courses(course courses[]) {
ifstream inf("courses.txt");
if (! inf) {
cerr << "Could not open courses.txt" << endl;
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUM_COURSES; i++) {
inf >> courses
}
inf.close();
}
---------------------------------------------------------
There are the outputs:
------ Build started: Project: Exercise3, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\lxy\my documents\visual studio 2005\projects\exercise3\exercise3\course.h(17) : error C2061: syntax error : identifier 'MAX_LINES'
c:\documents and settings\lxy\my documents\visual studio 2005\projects\exercise3\exercise3\course.h(32) : error C3867: 'course::description': function call missing argument list; use '&course::description' to create a pointer to member
c:\documents and settings\lxy\my documents\visual studio 2005\projects\exercise3\exercise3\course.h(32) : error C2109: subscript requires array or pointer type
c:\documents and settings\lxy\my documents\visual studio 2005\projects\exercise3\exercise3\course.h(32) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Build log was saved at "file://c:\Documents and Settings\LXY\My Documents\Visual Studio 2005\Projects\Exercise3\Exercise3\Debug\BuildLog.htm"
Exercise3 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

HELP ME
KimMPS
Just like you have to use the initializer list (that colon thingy in the constructor) to set the initial string of name and title, so must you use the initializer list to set the initial number of descripion elements.
course() : name(""), title(""), description(MAX_LINES) {}
(I think this might solve your other errors too).