why am I getting this error: l-value specifies const object
here is the code;-
.h file
.cpp fileCode:// String.h: interface for the String class. // ////////////////////////////////////////////////////////////////////// #include<stdio.h> #include<iostream> using namespace std; #if !defined(AFX_STRING_H__B89E5B66_2BB2_467B_B470_66ED708714DC__INCLUDED_) #define AFX_STRING_H__B89E5B66_2BB2_467B_B470_66ED708714DC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class String { public: String(const char* = "");//done String(const String&);//done virtual ~String();//done bool operator !()const;//done char &operator[](const int)const; String operator +(const char*)const;//done String operator +(const String &)const;//done void operator = (const char*);//done const char* getString() const;//done private: char *stringPtr ; int length; void setString(const char*); }; istream &operator >>(istream&, String&); ostream &operator <<(ostream&, const String&); class OutOfRange { public: OutOfRange(int s, int p):size(s),position(p) { } const char* getError() { static char error[200]; sprintf(error,"20th char in the string is Access attempt position %d on string of size %d\n",position, size); return error; } private: int size; int position; }; #endif // !defined(AFX_STRING_H__B89E5B66_2BB2_467B_B470_66ED708714DC__INCLUDED_)
main.cppCode:// String.cpp: implementation of the String class. // ////////////////////////////////////////////////////////////////////// #include "String.h" #include<string.h> #include<assert.h> #include<iostream> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// String::String(const char* s):length(strlen(s)) { setString(s); } void String::setString(const char *string1) { stringPtr = new char[length + 1]; //length is of string1, the current obj assert(stringPtr != 0); //terminate if memory not allocated strcpy(stringPtr, string1); } String::String(const String &other):length(other.length) { setString(other.stringPtr); } const char* String::getString()const { return stringPtr; } String::~String() { delete[] stringPtr; } void String::operator =(const char* newString) { if(stringPtr) delete[] stringPtr; if(stringPtr!='\0') { stringPtr= new char[strlen(newString)+1]; strcpy(stringPtr,newString); length=strlen(newString); } } String String::operator +(const char *string2)const { char* buffer = new char[strlen(string2) + length + 1]; strcat(strcpy(buffer,stringPtr),string2); String temporary = buffer; delete[] buffer; return temporary; } String String::operator + (const String& object)const { return operator +(object.stringPtr); } char& String::operator [](const int k)const { if(k<0 || k>=length)throw OutOfRange(length,k); else return stringPtr[k]; } bool String::operator !()const { return length==false; } istream& operator>>(istream& in, String& obj1) { char buffer[80]; in >> buffer; obj1 = buffer; return in; } ostream& operator<<(ostream& out, const String& obj2) { out << obj2.getString(); return out; }
Code:#include <iostream> #include "String.h" using namespace std; void checkString(String obj) { cout << "Given String is "; if (!obj) cout << "empty\n"; else cout << " not empty\n"; } void printString(const String& stringObj) { cout << stringObj << endl; try { cout << "First char in the string is " << stringObj[0] << endl; } catch(OutOfRange& exceptionObj) { cout << exceptionObj.getError() << endl; } try { cout << "20th char in the string is "; cout << stringObj[19] << endl; } catch(OutOfRange& exceptionObj) { cout << exceptionObj.getError() << endl; } } void main() { const String hello = "Hello!"; String greet = hello + " there "; printString(greet); cout << "Now you may create your own string:\n"; String myString; cin >> myString; cout << "You entered:"; printString(myString); String bigString = greet + myString; printString(bigString); try { greet[1] = 'i'; } catch(OutOfRange& exceptionObj) { cout << exceptionObj.getError() << endl; } printString(greet); checkString(greet); String eString; checkString(eString); }



LinkBack URL
About LinkBacks



CornedBee