![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works The ErrorClass class maintains an array of error descriptions that are read from an error file. These error messages are sequential starting at 0 and going as high as 100 for this small example. The class definition of the ErrorClass class is provided in Listing 10.1 and the implementation file (.cpp) is given in Listing 10.2. This sample code is also provided on the CD that comes with this book. The important methods of this class will be discussed in turn. Listing 10.1 ErrorClass Class Definition File // // ErrorClass.h - definition of ErrorClass class // #include <iostream.h> #define ERRDESC_SIZE 81 #define MAX_ERRNUM 100 class ErrorClass { public: ErrorClass(); virtual ~ErrorClass(); // Utility methods const char* description() const; const char* description(int errorNumber) const; void currentError(int errorNumber); int currentError(void) const; bool isError() const; bool loadErrors(const char* errorFile); protected: int _currentError; bool _openError; char _errors[MAX_ERRNUM][ERRDESC_SIZE]; }; Listing 10.2 ErrorClass Class Implementation // // ErrorClass.cpp - implementation of ErrorClass class // #include <stdio.h> #include <stdlib.h> #include <string.h> #include ErrorClass.h ErrorClass::ErrorClass() : _currentError(-1), _openError(false) { } ErrorClass::~ErrorClass() { } const char* ErrorClass::description() const { return description(_currentError); } const char* ErrorClass::description(int errorNumber) const { if (_openError) return Error loading errors list; if (_errors[0][0] == \0 || errorNumber >= MAX_ERRNUM) return ; return _errors[errorNumber]; } void ErrorClass::currentError(int errorNumber) { _currentError = errorNumber; } int ErrorClass::currentError() const { return _currentError; } bool ErrorClass::isError() const { if (_openError) return true; return (_currentError >= 0); } bool ErrorClass::loadErrors(const char* errorFile) { FILE* fptr = fopen(errorFile, r); if (fptr == NULL) { _openError = true; return false; } else { _openError = false; for (int j = 0; !feof(fptr); j++) { char buf[ERRDESC_SIZE]; // Read the error descriptions from the // error file. The error descriptions must // be in sequential order. // if (fgets(buf, ERRDESC_SIZE, fptr) != NULL) strcpy(_errors[j], buf); } fclose(fptr); } return true; }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|