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 works fine when its in a project with just these files. I have 2 problems: 1 is with the ...

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

    Custom String class gives problem with another prog.

    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)
    Quote Originally Posted by MS Visual C++ 6.0 Compilier
    error C2143: syntax error : missing ';' before '&'
    (22) : error C2433: 'ostream' : 'friend' not permitted on data declarations
    (22) : error C2501: 'ostream' : missing storage-class or type specifiers
    (22) : error C2244: 'ostream' : unable to resolve function overload
    (22) : error C2061: syntax error : identifier 'ostream'
    (23) : error C2501: '<<' : missing storage-class or type specifiers
    (23) : error C2805: binary 'operator <<' has too few parameters
    (23) : error C2333: '<<' : error in function declaration; skipping function body
    (28) : error C2143: syntax error : missing ';' before '&'
    (28): error C2433: 'istream' : 'friend' not permitted on data declarations
    (28) : error C2501: 'istream' : missing storage-class or type specifiers
    (28) : error C2244: 'istream' : unable to resolve function overload
    (28) : error C2061: syntax error : identifier 'istream'
    (29) : error C2501: '>>' : missing storage-class or type specifiers
    (29) : error C2805: binary 'operator >>' has too few parameters
    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 :/

    String.h:
    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;
    	}			
    };
    String.cpp:
    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;
    		}
    }
    
    //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;
    }
    */
    Main.cpp
    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;
    }
    Last edited by I BLcK I; 12-18-2006 at 03:33 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) Since the string definition uses the streams, String.h should include <iostream>.
    2) Can you indicate the lines where the errors occur?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    When i include iostream in String.h, it still gives the same errors
    Code:
    	friend ostream& operator << (ostream &os, String &s)     //line 23 - this line 
    	{
    		os << s.pch;
    		return os;
    	}
    	
    	friend istream& operator >> (istream &is, String &s)   //line 28 - and this line cause errors
           {                                                                                   //line 29 Not sure why its complaining here
             ....
           }
    Last edited by I BLcK I; 12-18-2006 at 03:24 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >When i include iostream in String.h, it still gives the same errors
    Did you add 'using namespace std'? That's a bad practice in headers, but it's a quick way to verify that your problem is what we think it is.
    My best code is written with the delete key.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you implementing these operators both in the class definition and separately? The class declaration should only have a line saying that this operator overload is a friend function.

    What you have commented out in String class, is basically right. You might just add to << operator that the String reference is const.

    And delete the overload implementations from the class declaration. These are not (can't be) member functions.
    Last edited by anon; 12-18-2006 at 04:34 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by Prelude
    >When i include iostream in String.h, it still gives the same errors
    Did you add 'using namespace std'? That's a bad practice in headers, but it's a quick way to verify that your problem is what we think it is.
    putting using namespace std; in string .h made them the errors disappear. im sort of confused on this namespace std. could you please explain what the cause of the problem is?

    Quote Originally Posted by anon
    Are you implementing these operators both in the class definition and separately? The class declaration should only have a line saying that this operator overload is a friend function.

    What you have commented out in String class, is basically right. You might just add to << operator that the String reference is const.

    And delete the overload implementations from the class declaration. These are not (can't be) member functions.
    my intention was to put them in string.cpp and not inline but when i placed them in string.cpp i got errors about the operators being unable to access private data, so i asked for help and suggested to place them in string.h which solved the problem for the main.cpp above. however this kinda backfired on me while i tried to use this custom string class for nother program.

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