From: iwaki Newsgroups: comp.os.msdos.djgpp Subject: Re: why do i get this compiler error? Date: 8 Dec 1997 04:34:23 GMT Organization: iwaki Lines: 415 Message-ID: <66ftcf$8fq$1@gte2.gte.net> References: <65q40m$6f3 AT camel12 DOT mindspring DOT com> Reply-To: iwaki AT gte DOT net NNTP-Posting-Host: 1cust142.tnt4.lax3.da.uu.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk michael wrote: > > dear all c/ c++ programmers > i have been having problem trying to compile this program for the > last three days. > the program has been wriiten correctly, there are no syntax errors. But the > every time i compile it, the compiler gives me a parse error for line after > private in the class definition below in addition to a laundry list of more > errors all refering to string type. . if anyone knows how I can fix this > problem and why this occurs please send me a reply e-mail. I can't > understand why this error occurs the program below is from a c++ book, the > author claims that all the source programs in the book have been tested on a > variety of platforms and compilers and that all are should work. once again > i would appriaciate all the help I can get. If you know how to make this > work or any revisions that are neccessary please reply to me. > // pTest.cpp > #include "plisting.h" > int main() { > phoneListing list[3]; > > list[0] = phoneListing("Hall", "Rob", "555-1234"); > list[1] = phoneListing("Esterly", "Anna", "555-9999"); > list[2] = phoneListing("Ching", "June", "555-8888"); > cout << list[0].firstName() << endl; > cout << list[1].lastName() << endl; > cout << list[2].phoneNumber() << endl; > for(int j = 0; j < 3; j++) > list[j].display(); > return 0; > } > ---------------------------------------------------------------------------- > --------------------------------------- > // Specification file: plisting.h > // declaring class phonelisting > #ifndef PLISTING_H > #define PLISTING_H > #include > class phoneListing { > public: > > phoneListing::phoneListing(); > // POST: A phoneListing object is constructed without setting state > > phoneListing::phoneListing(string initLast, string initFirst, > string initNumber); > void phoneListing::display(); > string phoneListing::lastName(); > string phoneListing::firstName(); > string phoneListing::phoneNumber(); > private: > string ln, fn pn; // the compiler keeps refering to this line, that > the parse error > // occurs before the semi colon... > }; > #include "plisting.cpp" > #endif > ---------------------------------------------------------------------------- > ---------------------------------- > // implementation file: plisting.cpp > // implements class phoneListing > phoneListing::phoneListing() > { > // default constructor > } > phoneListing::phoneListing(string initLast, string initFirst, > string initNumber) > { > ln = initLast; > ln.toUpper(); > fn = initFirst; > fn.toUpper(); > pn = initNumber; > } > string phoneListing::lastName() > { > return ln; > } > string phoneListing::firstName() > { > return fn; > } > string phoneListing::phoneNumber() > { > return pn; > } > void phoneListing::display() > { > cout.width(15); > cout << firstName() << ' ' << lastName(); > cout.width(20-lastName().length()); > cout << ' ' << phoneNumber() <, endl; > } > > --------------------------------------------------------------------------------------------------------- > > //-------------------------------------------------------------------- > // SPECIFICATION FILE: ourstr.h > // > // Declares: 1. class string > // 2. The following overloaded operator functions > // << output > // >> input > // < > >= <= (relational operators) > // == != (equality operators) > // + (ops must be string objects or 1 string literal > //-------------------------------------------------------------------- > #ifndef OURSTR_H // Avoid duplicate compilation > #define OURSTR_H > #include // for class istream and class ostream > class string { > public: > // Default Constructor: > string::string(); > // POST: String object is set to default null string as in string a; > // Copy constructor: > string::string(const string & strObj); > // POST: Object is copied during argument/parameter associations > // and during a function return. Objec > t is also initialized as > // string anotherStr(aStr); > // Initializer constructor: > string::string(const char * initText); > // POST: Object is initialized using char* argument when constructed > // as string aStr ("Hall"); > // Destructors, such as ~string, are invoked to deallocate memory > // automatically when the string object is no longer necessary > ~string(); > // Qualifying ~string with string:: causes problems on some compilers > void string::toUpper(); > // POST: The string is converted to upper case. > // Overload the assignment operator for string objects and char * > string & string::operator = (const char * rightValue); > // POST: The lvalue string object stores the string literal and > // the dynamic length of the expression to the right of = > // as in s1 = "A string literal" > string & string::operator = (const string & rightValue); > // POST: The lvalue string object stores the string literal and > // the dynamic length of the expression to the right of = > // as in s1 = s2; > // Concatenation operator: > string & string::operator += (const string & right); > // POST: The right string object is appended to the > // string object on the left. > // Access individual characters as in strVar[3] > char & string::operator [] (int index); > // POST: A reference to one individual character is returned. > // This may be applied to non-const objects. > // Access individual characters as in strVar[3] > const char & string::operator [] (int index) const; > // POST: A reference to one individual character is returned. > // This may be applied to const objects. > // Accessor function: > unsigned int string::length() const; > // POST: The dynamic length of the string object is returned > // Accessor function: > char * string::chars() const; > // POST: The characters (char *) of the string object are returned > // Could use as arg to ifstream inFile(fileName.theChars()); > private: > int len; // The length of any one object > char * theChars; // A pointer to the characters of a string object > }; // End the string class declaration > //-------------------------------------------------------------------- > // Allow easy input and output of strings for standard I/O (cin/cout) > // and simple file I/O with ifstream and ofstream objects only > //-------------------------------------------------------------------- > ostream & operator << (ostream & os, const string & outputString); > istream & operator >> (istream & is, string & inputString); > //-------------------------------------------------------------------- > // Allow catenation and comparison of string objects > //-------------------------------------------------------------------- > string operator + (const string & left, const string & right); > string operator + (const string & left, const char * right); > string operator + (const char * left, const string & right); > //-------------------------------------------------------------------- > // The overloaded relational operator function prototypes > //-------------------------------------------------------------------- > int operator < (const string & left, const string & right); > int operator <= (const string & left, const string & right); > int operator > (const string & left, const string & right); > int operator >= (const string & left, const string & right); > //-------------------------------------------------------------------- > // The overloaded equality operator function prototypes > //-------------------------------------------------------------------- > int operator == (const string & left, const string & right); > int operator != (const string & left, const string & right); > #include "ourstr.cpp" // Add member function implementations > #endif // OURSTR_H > > ---------------------------------------------------------------------------- > -------------------------------- > > //---------------------------------------------------------------------- > // IMPLEMENTATION file ourstr.cpp > // > // Implements: class string > // Note: This file is automatically included by ourstr.h > //---------------------------------------------------------------------- > #include // for strlen and strcpy > #include // for exit > #include // for class istream and class ostream > #include // for toupper > string::string() > { // Initialize a string object as a null string in string a; > len = 0; > theChars = new char[1]; > theChars[0] = 0; // Store the null char into the > // first byte pointed to by theChars > } > string::string(const char * initText) > { // Initialize string objects as: > // string strVar ("Initial string"); > // string strVar = "Initial string"; > len = strlen(initText); // Actual length without '\0' > theChars = new char[len + 1]; // Allocate memory + 1 for '\0' > if(! theChars) > { // Terminate program if no more memory > cout << "**Error** Memory exausted trying to create " > << initText << endl > << "...Program terminated..." << endl; > exit(0); > } > strcpy(theChars, initText); // Let theChars point to initText > } > string::string(const string & source) > { // This is the copy constructor which allows string b(a); > len = source.len; > theChars = new char[len + 1]; // Allocate memory. > if(! theChars) > { // Terminate program if no more memory > cout << "**Error** Memory exausted trying to create " > << source.theChars << endl > << "...Program terminated..." << endl; > exit(0); > } > strcpy(theChars, source.theChars); // Let theChars point to the chars > // of the copied object. > } > unsigned int string::length() const > { > return len; > } > char * string::chars() const > { > return theChars; > } > string::~string() > { // Deallocate memory to avoid exhausting the free store > delete [] theChars; > } > // Upcase the enter string as a member function strVar.toUpper(); > void string::toUpper() > { > int lastChar = length(); > for(int j = 0; j <= lastChar-1; j++) > theChars[j] = toupper(theChars[j]); > } > string & string::operator = (const string & rightValue) > { > // Skip copying in statements like this: aString = aString; > if (this != &rightValue) > { > delete theChars; // Deallocate memory of old string object > len = rightValue.len; > theChars = new char[len + 1]; > strcpy(theChars, rightValue.theChars); > } > return *this; > } > string & string::operator = (const char * rightValue) > { > delete theChars; // Deallocate memeory for the lValue object > len = strlen(rightValue); > theChars = new char[len + 1]; > strcpy(theChars, rightValue); > return *this; > } > string & string::operator += (const string & right) > { // Concatenation > int combinedLength = this->len + right.len; > // Allocate enough memory for both operands. > char * temp = new char[combinedLength + 1]; > if(! temp) > { // Terminate program if no memory left > cout << "**Error** Memory exausted trying to create " > << right.theChars << endl; > exit(0); > } > strcpy(temp, theChars); // 1. Create the > strcat(temp, right.theChars); // new string > delete theChars; // 2. Deallocate unneeded memory > // 3. Point theChars to the same memory as temp > theChars = temp; // 4. Create and return > len = combinedLength; // this newly > return *this; // created object > } > char & string::operator [] (int index) > { > // The user refers to the first character > // in the string object with [1], not 0. > if((index < 0) || (index >= len)) > { > cout << "\n**Error** For string object `" << theChars > << "', subscript [" << index << "] is out of range. " << endl; > cout << "Subscript must be in the range of 1.." << (len-1) << endl; > exit(0); > } > return theChars[index]; > } > const char & string::operator [] (int index) const > { > if((index < 0) || (index > len)) > { > cout << "\n**Error** For string object `" << theChars > << "', index [" << index << "] is out of range. " << endl; > cout << "Index must be in the range of 1.." << (len-1) << endl; > exit(0); > } > return theChars[index]; > } > ostream & operator << (ostream & os, const string & outputString) > { > os << outputString.chars(); // Insert characters to output stream. > return os; > } > istream & operator >> (istream & is, string & inputString) > { > char temp[128]; // Allow a very large input string > is >> temp; // Read chars up to first whitespace. > inputString = string(temp); // Alter argument to right of >> > return is; > } > string operator + (const string & left, const string & right) > { // concatenation such as strVar + strVar > string temp(left); // Create new object with left operand. > temp += right; // Use the existing operation to create > return temp; // one string from two string operands. > } > string operator + (const string & left, const char * right) > { // Concatenate as strVar + "a string"; // constant on right > string temp(left); > temp += right; > return temp; > } > string operator + (const char * left, const string & right) > { // Concatenation such as "a string" + strVar; // constant on left > string temp; > temp = left; > temp += right; > return temp; > } > int operator < (const string & left, const string & right) > { > return (strcmp(left.chars(), right.chars()) < 0); > } > int operator <= (const string & left, const string & right) > { > return ! (strcmp(left.chars(), right.chars()) > 0); > } > int operator > (const string & left, const string & right) > { > return (strcmp(left.chars(), right.chars()) > 0); > } > int operator >= (const string & left, const string & right) > { > return ! (strcmp(left.chars(), right.chars()) < 0); > } > int operator == (const string & left, const string & right) > { > return (strcmp(left.chars(), right.chars()) == 0); > } > int operator != (const string & left, const string & right) > { > return (strcmp(left.chars(), right.chars()) != 0); > } > > the above are all the files in the program. as noted above the parse error > occurs in the include file plisting.h, in the line after private, where > the private data > members are declared. My hunch is that the compiler does not recognize the > string > type from the class string. > thank you > Mitchell Jordan > Mjordan2 AT pipeline DOT com I don't know much about C/C++. I starting to learn. This may sound real silly but could it be that the '{' after the int(main) shouldn't be on same line? You have: int main() { Should'nt it be: int main() { ... ... } I really don't know about any other errors. Perhaps other users could suggest something. Good Luck Michael Iwaki iwaki AT gte DOT net --