Problem with operator<<

Hey guys im just writing a program that works like cout. Instead of typing;
cout << "abc";
I have to write;
spy << "abc";
and make it work the same way as cout.

I have spy << "abc"; working and its displaying but when i pass a double ie: spy << d1; (where d1 is a double) it doesnt work.

Here is the class i wrote so far;
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
#ifndef SPYOUTPUT
#define SPYOUTPUT

#include<iostream>
using namespace std;

class SpyOutput
{

public:
	SpyOutput ();
	SpyOutput (std::ostream * k);
	~SpyOutput ();
	SpyOutput operator<<(void* value);
};

SpyOutput::SpyOutput () {
	//cout << "default constructor" << endl;
}

SpyOutput::SpyOutput (std::ostream * k) {
	//cout << k << endl;
}

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

SpyOutput SpyOutput::operator<<(void* value){
	SpyOutput temp;
	int sf = sizeof(value);  
	cout << (char*)value;
	return (temp);
}

#endif 


This is 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??
You are passing the double as a void *, not good. You will need either create a function for each type or make a template function and just pass it directly to std::cout.
Ok i added a operator for doubles;
1
2
3
4
5
6
SpyOutput SpyOutput::operator<<(double value){
	SpyOutput temp;
	//int sf = sizeof(value);  
	cout << value;
	return (temp);
}


and its working better now. Do you think it would be better to use a template function though??
This is what my class looks like now;
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
#ifndef SPYOUTPUT
#define SPYOUTPUT

#include<iostream>
using namespace std;

class SpyOutput
{

public:
	SpyOutput ();
	SpyOutput (std::ostream * k);
	~SpyOutput ();
	SpyOutput operator<<(char *value);
	SpyOutput operator<<(int value);
	SpyOutput operator<<(double value);
};

SpyOutput::SpyOutput () {
	//cout << "default constructor" << endl;
}

SpyOutput::SpyOutput (std::ostream * k) {
	//cout << k << endl;
}

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

SpyOutput SpyOutput::operator<<(char *value){
	SpyOutput temp;
	cout << value;
	return (temp);
}

SpyOutput SpyOutput::operator<<(int value){
	SpyOutput temp;
	cout << value;
	return (temp);
}

SpyOutput SpyOutput::operator<<(double value){
	SpyOutput temp;
	cout << value;
	return (temp);
}

#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();
}


the output im getting is
abc10d1=12.3 i1=4512210


It looks like when i use single quotations its showing the ASCII value??

Any way around this??
It's casting to int. You don't have an operator<<(char)
Yes, templates will be better. Think of a way to manage classes that overload operator<< with ostreams
Heres my problem right now:
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
#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);
};

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

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

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

SpyOutput SpyOutput::operator<<(char *value){
	SpyOutput temp;

	stringstream ss;
	ss << value; //builds a string stream

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

	count = count + s.length();

	cout << s << " lenght: " << count << endl;
	return (temp);
}

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

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

	count = count + s.length();

	cout << s << " lenght: " << count << endl;
	return (temp);
}

SpyOutput SpyOutput::operator<<(int value){
	SpyOutput temp;
	//char* temp_val = (char*)value;
	//count += strlen(temp_val);
	cout << value /*<< count*/;
	return (temp);
}

SpyOutput SpyOutput::operator<<(double value){
	SpyOutput temp;
	cout << value;
	return (temp);
}

#endif 


Right now SpyOutput SpyOutput::operator<<(char value) and SpyOutput SpyOutput::operator<<(char* value) is what i am working on.

This is 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();
}


the int count found in my class is what im using to keep track of characters being used but its not keeping track correctly. I think maybe im using the int count incorrectly if anyone can suggest anything?
I think this is your mistake
SpyOutput SpyOutput::operator<<(char *value){ //you are returning a copy

Change it to
1
2
3
4
SpyOutput& SpyOutput::operator<<(char *value){
//...
  return *this;
}
Last edited on
Ok thanks so this is usually how you use operators?
@ne555: if you're returning the caller's value, why use SpyOutput& and not SpyOutput?

@matthwefs: what's the effect of using:
1
2
3
SpyOutput::SpyOutput (std::ostream * k) {
	//empty body here...
}


for me, it doesn't have any effect... i don't understand...
Last edited on
The idea is to continue working with the same object.
spy << "oh " << "brave "<<"new "<<"world"; If you return a copy then you will be updating the count variable of the copy (in the example spy.count == 3)
Topic archived. No new replies allowed.