14 Replies - 1379 Views - Last Post: 31 March 2008 - 05:08 AM Rate Topic: -----

#1 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Operator overloading of << and >>

Posted 26 March 2008 - 06:13 AM

In my code I have two classes (Word and Sentence).
I need to overload (in Word class) the operator << to print the word.(I'm not allowed to use strings, must use char* for strings). My code for << operator is :
friend ostream& operator<<(ostream &os, Word &w)
{
for (int i=0;i<w.len;i++)
os<<w.t_word[i];
os<<" ";
return os;
}


the error the compiler give me is :
ISO C++ forbids declaration of ostream with no type.
The compiler shows me the problem on declaration (in Word.h) not realization.

For the >> operator: I need it to get several words with spaces between them, and reallocate an array to add them. I currently have a "realloc" function:
void realloc (int size, Word** words)
{
Word** temp=new Word* [num_of_words];
for (int i=0;i<num_of_words;i++)
temp[i]=words[i];
delete [] words;
num_of_words=size;
words=new Word* [num_of_words];
for ( i=0;i<num_of_words;i++)
words[i]=temp[i];
}


which can increase the size, but my problem is determining how many words are there
(The input should look like this "asa va lsa jka jwa" before each space is a Word type object).
I thought of the following loop, but I think it may have a problem in it
while (*inp){
if ((*inp)==" ")
sp_counter++;
}



Also another problem I have is with preforming bubblesort on an array of strings.
(I am not allowed to use string object, only array of characters)
I need to lexographically arrange an array, does anyone has a clue on how to apply bubblesort to strings?
Thanks in advance for the help.

This post has been edited by Sepanto: 26 March 2008 - 06:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Operator overloading of << and >>

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: Operator overloading of << and >>

Posted 26 March 2008 - 10:49 AM

I don't have my compiler, but:

Quote

the error the compiler give me is :
ISO C++ forbids declaration of ostream with no type.


It is a friend function declared in your class, but it should STILL have a type.

eg:
class cSomeClass
{
	friend ostream& operator<<(ostream &os, Word &w)
	. . .
}
. . .

//Definition:
int ostream& operator<<(ostream &os, Word &w)
{
	. . .
}


All functions HAVE to return a type, and friend isn't a type - it's more like a reference

This post has been edited by gabehabe: 26 March 2008 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: Operator overloading of << and >>

Posted 26 March 2008 - 11:31 AM

View Postgabehabe, on 26 Mar, 2008 - 11:49 AM, said:

//Definition:
int ostream& operator<<(ostream &os, Word &w)
{
	. . .
}


I'm pretty sure ostream& is the return type.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: Operator overloading of << and >>

Posted 26 March 2008 - 11:44 AM

oh well...

anyhow, friend shouldn't be used in the definition of the function, its only used when it's declared within the class so that the compiler knows that this function can have access to the classes private members.
Was This Post Helpful? 0
  • +
  • -

#5 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 26 March 2008 - 12:40 PM

So how do i fix it?
I change
friend ostream& operator<<(ostream& os, Word w1) ?
it does have a return type :
it returns ostream referece.

This post has been edited by Sepanto: 26 March 2008 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 27 March 2008 - 11:55 AM

I hate doubling... but i really really need the help!
Was This Post Helpful? 0
  • +
  • -

#7 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 28 March 2008 - 08:44 AM

I've got no idea for the input operator (>>) gimme even an algorithem and i'll program it.
p.s. : Is the realloc good?
this what i made so far for the input operator, i wanna know if you think it's good.
friend istream& operator>> (istream& is,Sentence s1){
char buffer[80],temp[80];
int i=0;
    is.getline(buffer,80);
while (buffer[i]!='\0'){
	while (buffer[i]!=' ')
		temp[i]=buffer[i];
	is>>temp[i];
	if ((buffer[i]==' ')&&(buffer[i+1]!=' '))
	num_of_words++;
	i++;
            }
}


This post has been edited by Sepanto: 28 March 2008 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: Operator overloading of << and >>

Posted 28 March 2008 - 09:47 AM

Just to clarify, I don't think that
void a_function(void)
has a return and
a &istream is a type.
Was This Post Helpful? 0
  • +
  • -

#9 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 28 March 2008 - 11:02 AM

It's not void, it's return type is istream adresss (istream&)
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Operator overloading of << and >>

Posted 28 March 2008 - 07:58 PM

Check this out:

Operator Overloading

If that doesn't help I'll be back later today.
Was This Post Helpful? 0
  • +
  • -

#11 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 29 March 2008 - 07:38 AM

thanks for the help! I need more help designing an algorithem for >> operator (I need to get a line, and add each word in it to a new word type variable, Every 2 word have a ' ' between them.)
this is my code so far :
istream& operator>> (istream& is,Sentence s1)
{

char buffer[80],temp[80];
int i=0;
    is.getline(buffer,80);
	while (buffer[i]!='\0')
	{
	while (buffer[i]!=' '){
		temp[i]=buffer[i];
		i++}
	for (int j=0;j<i;j++,i--)
	is>>temp[j];
	if ((buffer[i]==' ')&&(buffer[i+1]!=' '))
	num_of_words++;
	i++;
	}
	return is;
}



Was This Post Helpful? 0
  • +
  • -

#12 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 29 March 2008 - 09:26 AM

I solved my compiler problem by using #include <istream.h> and #include <ostream.h>. I still haven't managed to find an algorithem for getting the words .
Was This Post Helpful? 0
  • +
  • -

#13 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 30 March 2008 - 07:32 AM

Hi guys. I nearly solved all my problems, but now when compiling with visual studio c++ (6.0) I get the following errors :
u:\cpp\3\funcs.cpp(55) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class Sentence' (or there is no acceptable conversion)
u:\cpp\3\funcs.cpp(100) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Word' (or there is no acceptable conversion)
u:\cpp\3\funcs.cpp(103) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Sentence' (or there is no acceptable conversion)
the abovemention lines are :
void menu(Sentence s1)
{
int i,j;
int input;
char w[80];
Word w1("empty");
	do
		{
			std::cout<<"MENU"<<endl;
			std::cin>>input;  
			switch (input)
		{
				case 0:
				std::cin>>s1;//55
				break;
				case 1:
				std::cin>>w;
				w1=s1.string_to_word(w);
				s1.addWord(w1);
				break;
				case 2: 
				std::cin>>w;
					s1.removeWord(w);
				break;
				case 3: 
				s1.Sort();
				break;
				case 4:
					std::cin>>i>>j;
				if ((i<s1.get_num_of_words())&&(i>=0)&&(j<s1.get_num_of_words())&&(j>=0))
				//s1.concat(i,j);
				break;
				case 5: 
					std::cin>>i>>j;
				if ((i<s1.get_num_of_words())&&(i>=0)&&(j<s1.get_num_of_words())&&(j>=0))
						s1.newConcat(i,j);
				break;
				case 6:
					std::cin>>i;
				if ((i<s1.get_num_of_words())&&(i>=0))
						s1.reverse(i);
				break;
				case 7:
				std::cin>>i>>j;
				if ((i<s1.get_num_of_words())&&(i>=0)&&(j<s1.get_num_of_words())&&(j>=0))
						s1.copy(i,j);
				break;
				case 8:
					std::cin>>i>>j;
				if ((i<s1.get_num_of_words())&&(i>=0))
				{
					if ((j<=strlen(s1[i].get_word()))&&(j>=0))
	   						 s1.getChar(i,j);
				}
				break;
				case 9:
				std::cin>>i;
				if ((i<s1.get_num_of_words())&&(i>=0))
					std::cout<<s1[i];//100
				break;
				case 10:
					std::cout<<s1;//103
				break;
				case 11:
					std::cout<<"GoodBye!"<<endl;
				break;
			default:
			std::cout<<"Not available"<<endl;
			break;
			  }
	}while (input!=11);
}


the definitions of overloading the operators << and >> in Sentence are :
/******************************************************
*function name: operator <<.                          
*function input: an ostream object, a sentence.
*function output: an ostream object.
*function number :2.12
*function purpose:overload the << operator to allow printing of sentence as a 
*whole
*******************************************************/
ostream &operator<<(ostream &out,Sentence &s1)
{
	//2.12.1 looping and printing each word alone, and a space after each word.
	for (int i=0;i<s1.num_of_words;i++)
	{
		//2.12.2 i may do this having overloaded the << operator in Word class
		out<<*(s1.words[i]);
		out<<" ";
	}
	out<<endl;
return out;
}
/******************************************************
*function name: operator >>
*function input: an istream object, a sentence.
*function output: an istream object.
*function number :2.13
*function purpose:overload the >> operator to allow getting a few words 
* one after the other
*******************************************************/
istream& operator>> (istream& is,Sentence s1)
{
char buffer[80],*temp;
int i=0,j,k,l;
    is.getline(buffer,80);
	while (buffer[i]!='\n')
	{
		if (buffer[i]!=' ')	
			j=i;
		while (buffer[i]!=' ')
			i++;
		k=i-1;
		temp=new char [k-j];
			for (l=j;l<k;l++)
				temp[l-j]=buffer[l];
		Word w1(temp);
		s1.addWord(w1);
			i++;
	}
	return is;
}


and the definition of >> in Word is :
/*******************************************************
*function name :operator<<
*function input:an integer, an ostream member reference.
*functin output:an ostream reference.
*function number : 16
*function purpose:overload the << operator so I may use Word type variables
*with std::cout.
*******************************************************/
ostream& operator<<(ostream &os, Word &w)
{
	for (int i=0;i<w.len;i++)
	os<<w.t_word[i];
	os<<" ";
return os;
}


Sentence.cpp and words.cpp compile well alone, but funcs.cpp(the ifle in which void menu() exists) doesn't.
thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#14 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 30 March 2008 - 11:14 AM

does anyone see why the following method will give me a segmentation fault (compiled under g++)
void Sentence::removeWord(char* word)
{
//2.5.1temp_count- counter for temporary array - rem - remember num_of_words.
int rem=num_of_words,temp_count=0,j;
//2.5.2removing the occurnces of the word.
	if(findWord(word)!=-1)
	{
		cout<<findWord(word);
			words[findWord(word)]=NULL;	
		num_of_words--;
	}
	//2.5.3acllocating a new temporary array.
	Word** temp=new Word* [num_of_words];
	//2.5.4 for all the numbers in the array, if word[j] is not NULL, I put it in temp;
	for (j=0;j<rem;j++)
	{
		if (words[j]!=NULL)
		{
			temp[temp_count]=new Word (words[j]->get_word());
			temp_count++;
		}
	}
	//2.5.5 deleting words and reallocating it.		
	delete [] words;
	words=new Word* [num_of_words];
	for (j=0;j<num_of_words;j++)
		words[j]=new Word (temp[j]->get_word());
		delete []temp;
}


Was This Post Helpful? 0
  • +
  • -

#15 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Operator overloading of << and >>

Posted 31 March 2008 - 05:08 AM

View PostSepanto, on 30 Mar, 2008 - 11:14 AM, said:

does anyone see why the following method will give me a segmentation fault (compiled under g++)
void Sentence::removeWord(char* word)
{
//2.5.1temp_count- counter for temporary array - rem - remember num_of_words.
int rem=num_of_words,temp_count=0,j;
//2.5.2removing the occurnces of the word.
	if(findWord(word)!=-1)
	{
		cout<<findWord(word);
			words[findWord(word)]=NULL;	
		num_of_words--;
	}
	//2.5.3acllocating a new temporary array.
	Word** temp=new Word* [num_of_words];
	//2.5.4 for all the numbers in the array, if word[j] is not NULL, I put it in temp;
	for (j=0;j<rem;j++)
	{
		if (words[j]!=NULL)
		{
			temp[temp_count]=new Word (words[j]->get_word());
			temp_count++;
		}
	}
	//2.5.5 deleting words and reallocating it.		
	delete [] words;
	words=new Word* [num_of_words];
	for (j=0;j<num_of_words;j++)
		words[j]=new Word (temp[j]->get_word());
		delete []temp;
}


I solved the problem by changing the if to while, and changing null pointer to "!" :
	while(findWord(word)!=-1)
	{	
			words[findWord(word)]=new Word ("!");	
		l++;
	}


the NULL value to words[][] interupted with the findWord function, and this doesn't.

This post has been edited by Sepanto: 31 March 2008 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1