473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Operation Overloading

4 New Member
How would I overload the cout (<< ) operator in this function???

template <class T>
void Queue<T>::PrintQ()
{

cout << “The content of the array is as follows: ” << endl;

for (int i=0; i< theQ.size(); i++)

cout << theQ[i] << endl;
}


i tried to follow an example and came up with this... but i don't think this is right...

template <class T>
void Queue<T>::PrintQ()
{

cout << “The content of the array is as follows: ” << endl;

}



ostream& operator<<(ostream& os, PrintQ& theQ)
{
for (int i=0; i< theQ.size(); i++)

cout << theQ[i] << end;
return os;
}

please help... THANKS!
Oct 1 '06 #1
3 1722
m013690
23 New Member
In the function parameters list you listed the function name PrintQ. That should actually be a type (int, char, or in your case Queue). Then follow it with & theQ. Also, if you want to be able to use 'cout' in the normal way ( cout << theQ[c] ), your overloaded function cannot be a member function of the queue object, so unless the items you are printing are public members, the overloaded '<<' operator will not have direct access to the items being printed.

In general, though, your function is almost right on as it. Just change that one thing, and it looks fine.

Oh, and I saw one more thing. you said 'theQ[i]' but the queue object must have an array somewhere in it. It should be theQ.arrayName[i]. Your code would try to output the contents of theQ at index i, but that won't work unless theQ is an array itself, OR you have also overloaded the [ ] operator to work that way.
Oct 1 '06 #2
n355a
4 New Member
Thanks for the help but when i call the PrintQ function how does it know to also call the ostream& operator<< function???


template <class T>
void Queue<T>::PrintQ()
{

cout << “The content of the array is as follows: ” << endl;

}



ostream& operator<<(ostream& os, PrintQ& theQ)
{
for (int i=0; i< theQ.size(); i++)

cout << theQ[i] << end;
return os;
}
Oct 1 '06 #3
m013690
23 New Member
Thanks for the help but when i call the PrintQ function how does it know to also call the ostream& operator<< function???


template <class T>
void Queue<T>::PrintQ()
{

cout << “The content of the array is as follows: ” << endl;

}
by overloading the '<<' function, you're teaching the compiler how to output an entire Queue object to the screen. So, somewhere in your printQ function, you just need this line:

cout << theQ;

When the compiler sees this, it generates the following function call:

operator<< ( cout, theQ );

Inside the overloaded '<<' is where you put all the hard work of walking through the arrays of the Queue to write out individual elements which are of some base type (int, char, whatever).

That help?
Oct 1 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
9
by: | last post by:
The code below I suspect creates memory leaks: In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. After "c += 'new_object'" we have an unreferenced object which...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
16
by: foolsmart2005 | last post by:
ComplexNum ComplexNum::operator +(const ComplexNum& rhs) const // - operation is the similar way { ComplexNum ans; ans.real = real + rhs.real; ans.imaginary = imaginary + rhs.imaginary;...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.