Sign in to follow this  

overloading the stream insertion operator

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I am trying to overload the stream insertion operator just to help me to understand how the thing works. Below is smtg I wrote here now to explain my problem. As you see I seem to be overloaded the operator, and as long as I read my referance books that is the way it should be done ? The proplem is when I call the operator from the main like:
 cout << athing; 
, it also prints the private member variables, too. What I mean is:
 
athing test('A','B'); 
cout << athing; 
prints out ABHelloWorld while I was expecting to see it printing HelloWorld only. Would somebody please advice me before I go nuts? Thanks in advance.

class athing {

	private:
		char m_cVar1;
		char m_cVar2;

	public:
		athing();
		athing(char cVar1,char cVar2);
		~athing();	

        ostream& print(ostream& output);
};

inline ostream& operator<<(ostream& output,athing& m){return m.print(output);}


ostream& athing::print(ostream& output) {
	
	output << "Hello" << "World" << endl;
	return output;

int main(void) {
    athing test('A','B');
    cout << athing;
    return 0;
}

Share this post


Link to post
Share on other sites
The code you've posted doesn't actually compile. When patched to actually compile it only shows "HelloWorld". Show actual code that you're having the issue with.

Share this post


Link to post
Share on other sites
Thanks for your fast reply SiCrane,

Originally I was trying to write a class that represents a card from a Deck.
Here is my code:


//--------------
//File: card.hpp

#ifndef CARD_HPP
#define CARD_HPP

#include <stdio.h>
#include <ctype.h>
#include <iostream>

using std::ostream ;
using std::cout;
using std::cerr ;
using std::endl;

#include "main.hpp"
const char rankLabel[15] = {" A23456789TJQK"};

class card {

private:
char m_cSuit;
Rank m_iRank;

public:
card();
card (char cSuite,char cRank);
~card();

ostream& print(ostream& output);
};

inline ostream& operator<<(ostream& output,card& m){return m.print(output);}


#endif






//--------------
//File: card.cpp

#include "card.hpp"

card::card(){
}
card::card(char cSuite, char cRank) {

putchar(toupper(cRank));
putchar(toupper(cSuite));

switch(cSuite) {

case 'H':

case 'D':

case 'C':

case 'S':
m_cSuit = cSuite;
break;
default: //default to Hearts
m_cSuit='H';
break;
}//End of switch

switch(cRank) {

case ' ':
m_iRank=None;
break;
case 'A':
m_iRank=Ace;
break;
case '2':
m_iRank=Two;
break;
case '3':
m_iRank=Three;
break;
case '4':
m_iRank=Four;
break;
case '5':
m_iRank=Five;
break;
case '6':
m_iRank=Six;
break;
case '7':
m_iRank=Seven;
break;
case '8':
m_iRank=Eight;
break;
case '9':
m_iRank=Nine;
break;
case 'T':
m_iRank=Ten;
break;
case 'J':
m_iRank=Jack;
break;
case 'Q':
m_iRank=Queen;
break;
case 'K':
m_iRank=King;
break;
default:
m_iRank=None;
break;

}

}
card::~card() { cerr<<"-Destructor-\n";
}
ostream&
card::print(ostream& output) {

output << "Hello" << "World" << endl;
return output;
}






//--------------
//File: main.hpp

enum Rank {
None,
Ace,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
};






//--------------
//File: main.cpp

#include "card.hpp"

int main(void) {

char cSuit[4]={'H','D','C','S'};
card* myDeck[52];

for (int iSuiteRun = 0 ; iSuiteRun < 4; iSuiteRun++) {
for(int iDeckRun = Ace; iDeckRun <=King ;iDeckRun++) {
myDeck[iDeckRun] = new card(cSuit[iSuiteRun],rankLabel[iDeckRun]);
cout<<*myDeck[iDeckRun]<<endl;
}
}

return 0;
}





And here is what my output looks like :

AHHelloWorld

2HHelloWorld

3HHelloWorld

4HHelloWorld

... so on so on

What I expect to see here is the "HelloWorld" output only, but it also outputs private members of my class, or lets say my constructors parameters.

Thanks in advance.

Share this post


Link to post
Share on other sites
You might take another look at the documentation for putchar() (it looks like you might have the wrong idea about what it does).

Also, make sure you're using C++ headers consistently (rather than C headers). You might reconsider the use of 'type' prefixes for your variables as well (search the forum archives for 'Hungarian notation', and you should find more than a few threads on this topic).

There are some other improvements that could be made, but the above should get you started...

Share this post


Link to post
Share on other sites

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this