This works fine when its in a project with just these files. I have 2 problems: 1 is with the overloaded << and >> operators being inline and the compiler complains about not having access to pch even though its a friend function. My 2nd problem is that this works with main.cpp but not in any other program I write because it complains (in a different program- using this custom string class for)
Any ideas/suggestions to as what is going wrong? Im all out of ideas to why a friend function cant access data its allowed to :/Originally Posted by MS Visual C++ 6.0 Compilier
String.h:
String.cpp:Code:#include <stdlib.h> class String{ private: char *pch; int length; static char nul; public: String(); String(char *p); String(String const &s); ~String(); bool operator == (String s2) const; String operator = (String &s); String operator + (String &s) const; //friend ostream& operator << (ostream &os, String &s); //friend istream& operator >> (istream &is, String &s); friend ostream& operator << (ostream &os, String &s) { os << s.pch; return os; } friend istream& operator >> (istream &is, String &s) { if(s.pch != &String::nul) delete[] s.pch; char *ptemp = new char[1000]; is.getline(ptemp, 1000, '\n'); s.length = strlen(ptemp); s.pch = new char [s.length +1]; strcpy(s.pch, ptemp); return is; } };
Main.cppCode:#include <iostream> #include <stdlib.h> using namespace std; #include "String.h" char String::nul = '\0'; //default constructor String::String() { pch = 0; length = 0; } String::String(char *p) { if( p == 0 ){abort();} length = strlen(p); pch = (char*) malloc(length+1); strcpy(pch, p); } //copy constructor String::String(String const &str) { if (str.pch==0){ pch = 0; length = 0; } else{ length = str.length; pch = (char*) malloc(length+1); strcpy(pch, str.pch); } } //destructor String::~String() { delete[] pch; } //overloaded '==' bool String::operator == (String s2) const { if ( strcmp(pch, s2.pch) == 0 ) return true; else{ return false; } } //overloaded '=' String String::operator = (String &s) { String temp (s.pch); if (s.length > length) {delete [] pch;} //free old memory pch = new char[s.length]; length = s.length; strcpy (pch, s.pch); strcpy (temp.pch, s.pch); return temp; } //overloaded '+' String String::operator + (String &s) const { String temp; temp.length = length + s.length; if(temp.length == 0) return temp; temp.pch = new char[temp.length +1]; strcpy(temp.pch, pch); strcat(temp.pch, s.pch); return temp; } /*If i placed thethe overloaded << and >> operators in String.cpp ostream& operator << (ostream &os, String &s) { os << s.pch; return os; } istream& operator >> (istream &is, String &s) { if(s.pch != &String::nul) delete[] s.pch; char *ptemp = new char[1000]; is.getline(ptemp, 1000, '\n'); s.length = strlen(ptemp); s.pch = new char [s.length +1]; strcpy(s.pch, ptemp); return is; } */
Code:#include <iostream> #include "String.h" using namespace std; int main() { String s1, s3; String s2("This is a String"); cout << "Input a String: " << endl; cin >> s1; s1 = s2; if (s1 == s2) { cout << "s1 is equal to s2" << endl; } s3 = s1 + s2; cout << s3 << " = " <<s1 << " + " << s2 << endl; return 0; }



LinkBack URL
About LinkBacks



CornedBee