Custom String class gives problem with another prog.

This is a discussion on Custom String class gives problem with another prog. within the C++ Programming forums, part of the General Programming Boards category; This seems to work with the overloaded "<<" and ">>" operators placed in the header file, but complains about not ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    25

    Custom String class gives problem with another prog.

    This seems to work with the overloaded "<<" and ">>" operators placed in the header file, but complains about not being able to access pch even though its a friend function and i have no idea why its doing so. Main.cpp seems to not have any problems using this string class, but with another program it complains about the << and >> operators. Any ideas/suggestions?


    String.h as follows
    Code:
    #include <stdlib.h>
    class String{
    private:
    	char *pch;    //Pointer to a CHar array
    	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); - I would put these in string.cpp but the compiler complains about not being able to access pch
    	
    	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;
    	}			
    };
    String.cpp as follows
    Code:
    #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;
    		}
    }
    
    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;
    }

    Test module:
    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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    ack srry bout the double post i thought i lost all i typed when i hit the post thread button and got some Invalid thread specified error and thought "ahhh shhhhhht" ><

    id prefer this one to be used: Custom String class gives problem with another prog.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Problem with my NULL-terminated string class
    By elfjuice in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2006, 10:39 PM
  4. how get string vector from class in file?
    By tord in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2005, 10:58 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21