By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Operation Overloading

P: 4
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
Share this Question
Share on Google+
3 Replies


P: 23
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

P: 4
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

P: 23
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

Post your reply

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