Using "std::ostream" in my class

Problem lies in my class. I have a constructor where &cout is passed. In my constructor its
1
2
3
4
SpyOutput::SpyOutput (std::ostream * k) {
	*k << "something";
	count = 0;
}


*k displays
something
but i want to be able to use it in my operators also so i dont have to use cout like it did here for example:
1
2
3
4
5
6
7
8
9
10
11
12
SpyOutput& SpyOutput::operator<<(char *value){
	stringstream ss;
	ss << value; //builds a string stream

	std::string s; 
    s = ss.str(); 

	count = count + s.length();

	cout << s;
	return *this;
}


Here is the class header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef SPYOUTPUT
#define SPYOUTPUT

#include <strstream>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;

#define  SIZE_BUFF   1000

class SpyOutput
{
	int count;
	char membuff[SIZE_BUFF];
public:
	
	SpyOutput ();
	SpyOutput (std::ostream * k);
	~SpyOutput ();
	SpyOutput& operator<<(char *value);
	SpyOutput& operator<<(char value);
	SpyOutput& operator<<(int value);
	SpyOutput& operator<<(double value);
	int getCount();
};

SpyOutput::SpyOutput () {
	count = 0;
}

SpyOutput::SpyOutput (std::ostream * k) {
	*k << "something";
	count = 0;
}

SpyOutput::~SpyOutput () {
	//cout << "destructor";
}

SpyOutput& SpyOutput::operator<<(char *value){
	stringstream ss;
	ss << value; //builds a string stream

	std::string s; 
    s = ss.str(); 

	count = count + s.length();

	cout << s;
	return *this;
}

SpyOutput& SpyOutput::operator<<(char value){
	stringstream ss;
	ss << value; //builds a string stream

	std::string s; 
    s = ss.str(); 

	count = count + s.length();

	cout << s;
	return *this;
}

SpyOutput& SpyOutput::operator<<(int value){
	stringstream ss;
	ss << value; //builds a string stream

	std::string s; 
    s = ss.str(); 

	count = count + s.length();

	cout << s;
	return *this;
}

SpyOutput& SpyOutput::operator<<(double value){
	stringstream ss;
	ss << value; //builds a string stream

	std::string s; 
    s = ss.str(); 

	count = count + s.length();

	cout << s;
	return *this;
}

int SpyOutput::getCount(){
	return count;
}

#endif 


and the main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "SpyOutput.h"
#include<iostream>
#define endl   '\n'

void main()
{
   double d1 = 12.3;
   int i1 = 45;
   SpyOutput spy(&cout);
   spy << "abc" << endl;
   spy << "d1=" << d1 << " i1=" << i1 << 'z' << endl;

   cout << "count=" << spy.getCount() << endl;
   /*cout << "Check Sum=" << spy.getCheckSum() << endl;*/

   cin.get();
}


Any ideas guys??
You will need a pointer to a ostream, as a member of your class. It will be initialized in the constructor.
(a reference could work too, if you will not change where it points).

Edit: why don't you templatize you operator<< ?
Last edited on
I dont have much experience with templates as yet. I was going to try and have the program work first this way. So if i have time (this assignment is due this wednesday) i would research on templates and figure it out.

I did this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class SpyOutput
{
	int count;
	char membuff[SIZE_BUFF];
	std::ostream * spyout;
public:
	
	SpyOutput ();
	SpyOutput (std::ostream * k);
	~SpyOutput ();
	SpyOutput& operator<<(char *value);
	SpyOutput& operator<<(char value);
	SpyOutput& operator<<(int value);
	SpyOutput& operator<<(double value);
	int getCount();
};

SpyOutput::SpyOutput () {
	count = 0;
}

SpyOutput::SpyOutput (std::ostream * k) {
	*spyout = *k;
	*spyout << "something";
	count = 0;
}
....


Its not compiling though
Nevermind i foudn the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class SpyOutput
{
	int count;
	std::ostream* spyout;
public:
	
	SpyOutput ();
	SpyOutput (std::ostream *);
	~SpyOutput ();
	SpyOutput& operator<<(char *);
	SpyOutput& operator<<(char);
	SpyOutput& operator<<(int);
	SpyOutput& operator<<(double);
	int getCount();
};

SpyOutput::SpyOutput () {
	count = 0;
}

SpyOutput::SpyOutput (std::ostream * cout_) {
	spyout = cout_;
	count = 0;
}
Topic archived. No new replies allowed.