![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
Overloading >> and +
so im almost finished with my code but i am getting some error from my overloading istream and overloading + operator.
here is what each should do: operator>> //input the mystring object, delimit with white space characters I know my code for >> is pretty much complete garbage. operator+ //take two mystring objects. The string returned is the concatenation of the two arguments [HTML]#ifndef MY_STR #define MY_STR #include <iostream> #include <vector> using namespace std; class mystring { istream& getline (istream& in, mystring& target); friend bool operator < (const mystring& lhs, const mystring& rhs); friend bool operator > (const mystring& lhs, const mystring& rhs); friend bool operator >= (const mystring& lhs, const mystring& rhs); friend bool operator <= (const mystring& lhs, const mystring& rhs); friend bool operator == (const mystring& lhs, const mystring& rhs); friend bool operator != (const mystring& lhs, const mystring& rhs); friend mystring& operator + (const mystring& lhs, const mystring& rhs); private: char * sequence; unsigned int allocated; unsigned int curr_length; void reserve(unsigned int n, bool copy); public: mystring(const char str []=""); mystring(const mystring& source); ~mystring(){ delete [] sequence; } int length() const; char operator[] (int pos) const; mystring& operator=(const mystring & source); mystring& operator+=(const mystring & addend); mystring& operator+=(char addend); mystring& operator+=(const char addend[]); friend ostream& operator << (ostream& out, const mystring& rhs); friend istream& operator >> (istream& in, const mystring& rhs); }; //constructor mystring::mystring(const char str[]){ int i; sequence = NULL; allocated = 0; curr_length = 0; if ( str==" ") return; //reserve space - count char is string for(i=0; str[i]!='\0'; i++) reserve(i,false); //copy str to this string for(i=0;str[i]!='\0';i++) sequence[i] = str[i]; //update size curr_length=i; } mystring::mystring(const mystring &source){ //create the vector and initialize vSize, vCapacity, and vArr sequence = NULL; allocated = 0; curr_length = 0; //if size is 0, we're done if(source.curr_length==0) return; //if size is not 0, then reserve the space reserve(source.curr_length, false); //update curr_length curr_length = source.curr_length; //initialize the elements with source for(unsigned int i=0; i<source.curr_length; i++) sequence[i]=source.sequence[i]; } void mystring::reserve(unsigned int n, bool copy){ char* ptr=new char [n]; if(ptr==NULL){ cout<<"allocation failure"; exit(1); } if(copy){ for(unsigned int i = 0; i < curr_length; i++) ptr[i] = sequence[i]; } if(sequence!=NULL) delete [] sequence; sequence = ptr; allocated = n; } int mystring::length() const{ return curr_length; } char mystring::operator [](int pos) const{ return sequence[pos]; } mystring& mystring::operator=(const mystring& source){ //do we need more space? if(source.curr_length>allocated) reserve(source.curr_length, false); //update the size of the miniVec curr_length = source.curr_length; //copy source elements for(unsigned int i=0; i<curr_length; i++) sequence[i]=source.sequence[i]; //return the updated object return *this; } mystring& mystring::operator +=(const mystring &addend){ if(allocated<(curr_length+addend.length())) reserve(addend.length()+curr_length,true); for (int i=0; i<=addend.length(); i++){ sequence[curr_length] = addend[i]; curr_length++; } return *this; } mystring& mystring::operator +=(char addend){ if(allocated==curr_length) reserve(1+curr_length,true); sequence[curr_length]=addend; curr_length++; return *this; } mystring& mystring::operator +=(const char addend[]){ int i; for(i=0;addend[i]!='\0';i++); if(allocated<curr_length+i) reserve(curr_length+i,true); for(int j=0; j<=i; j++){ sequence[curr_length] = addend[j]; curr_length++; } return *this; } ostream& operator << (ostream& out, const mystring& rhs){ unsigned int i=0; while (i<rhs.curr_length){ out<<rhs.sequence[i]; i++; } } istream& operator >> (istream& in, const mystring& rhs){ rhs.curr_length=0; char ch; rhs=""; in.get(ch); while(!isspace(ch)){ rhs+=ch; in.get(ch); } return in; } istream& getline (istream& in, mystring& target){ //look for the new line char char ch; target=""; in.get(ch); while (ch!='/n'){ target+=ch; in.get(ch); } return in; } bool operator < (const mystring& lhs, const mystring& rhs){ //is lhs less than rhs unsigned int i=0; while(i<lhs.curr_length && i<rhs.curr_length){ if (lhs[i] > rhs[i]) return false; else if (lhs[i] < rhs[i]) return true; i++; } return (lhs.curr_length < rhs.curr_length); } bool operator > (const mystring& lhs, const mystring& rhs){ //is rhs less than lhs return rhs < lhs; } bool operator >= (const mystring& lhs, const mystring& rhs){ //is lhs less than or equal to rhs return (rhs > lhs || lhs == rhs); } bool operator <= (const mystring& lhs, const mystring& rhs){ //is rhs less than or equal to lhs return (rhs < lhs || lhs == rhs); } bool operator == (const mystring& lhs, const mystring& rhs){ int i=0; //compare lhs and rhs to see if they equal each other if (lhs.curr_length==rhs.curr_length){ if (lhs[i]==rhs[i]) return true; else return false; } else return false; } bool operator != (const mystring& lhs, const mystring& rhs){ return !(lhs==rhs); } mystring& operator + (const mystring lhs, const mystring rhs){ mystring holder=lhs; lhs+=rhs; return holder; } #endif[/HTML] |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>istream& operator >> (istream& in, const mystring& rhs){
You can't write to a const object. Remove the const qualifier from rhs. >ostream& operator << (ostream& out, const mystring& rhs){ You need to return the stream. >lhs+=rhs; And what meaning does += have when applied to a const object? Perhaps you meant to use holder instead of lhs.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
thanks for the help, i think i've got it now. I was just a bunch of stupid error on my part
|
|
|
|
|
|
#4 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>I was just a bunch of stupid error on my part
Hardly. Const-correctness is tricky to get right, especially with the standard library. That's one reason why most people don't bother with it.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| operator overloading | RemoteC2 | C++ | 12 | Aug 24th, 2006 8:54 AM |
| Function Overloading, Copy Constructors, Default Arguements | RemoteC2 | C++ | 5 | Aug 15th, 2006 1:09 PM |
| operator overloading question.. | sackarias | C++ | 8 | Mar 9th, 2006 4:30 PM |
| Overloading | jon182 | C++ | 0 | Mar 12th, 2005 10:09 PM |
| Operator Overloading issue [solved] | Dizzutch | C++ | 4 | Jan 29th, 2005 8:46 AM |