Member Functions

First I want to put it out there that yes this is homework and no I'm not asking for it to be done for me :) For my assignment I have to create a class that simulates blocks of memory. I have to write a few member functions and overload some operators. I have +, == and = working as they should but am having some problems with some other aspects of the assignment.

I am tying to overload the << operator. I am having some trouble with it and every example I can find online shows it as a friend function. I haven't seen it as a member function and I haven't figured it out yet so I am not sure how to proceed. To my understanding, a member function needs exactly one argument. I guess if anyone could point me to an example or an example of just the function signature that would help. Thanks for reading.
Overload it the same way you did the others.

void class_t::operator << (data_t data);
I tried something similar but I probably screwed up somewhere. Thanks :) I will keep trying.
> To my understanding, a member function needs exactly one argument
Nope, if it is a binary operator then the `left' operand is this (the object that makes the call)
so the signature just ask for one argument.

> I probably screwed up somewhere
Likely, if you show what you did we may help.
Ok this is what I have. I clearly don't understand something. Hopefully I am at least kind of on track.

This is the error message I get when trying to compile. For line 23 and 30.

Memory.cpp:23:19: error: ‘void Memory::operator<<()’ must take exactly one argument.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
#include <iostream>

using namespace std;

class Memory {	

	private:
								
		int *arr;
		int size;

	public:
		
		Memory();
		Memory(int size);
		
		void loadArray(int data);
		void dumpArray();
		int getSize();
		Memory operator+(const Memory other);
		Memory& operator=(const Memory other);
		bool operator==(const Memory other);
		void operator<<();	
};

Memory::Memory():Memory(10){};
Memory::Memory(int size): size(size), arr(new int [size]){};


void Memory::operator<<(){
	
	for ( int i = 0; i < this.getSize(); i++ ) {
	
		cout << "Location " << i << " : " << this.arr[i] << endl;
	}
}

bool Memory::operator==(const Memory other){
	
	if ( size != other.size ) return 0;

	for ( int i = 0; i < size; i++ ) {
	
		if ( arr[i] != other.arr[i] ) return 0;
	}
	
	return 1;

}

Memory& Memory::operator=(const Memory other){
	
	for ( int i = 0; i < getSize(); i++){
		
		arr[i] = other.arr[i];
	}

	return *this;
}

Memory Memory::operator+(const Memory other){
	
	Memory result;

	for ( int i = 0; i < getSize(); i++){
	
		result.arr[i] = arr[i] + other.arr[i];
	}
	
	return result;
}

void Memory::dumpArray(){
	
	cout << endl;

	for ( int i = 0; i < getSize(); i++ ) {
	
		cout << "Location " << i << " = " << arr[i] << endl;
	}
}

void Memory::loadArray ( int data ) {
	
	for ( int i = 0; i < getSize() ; i++ ){
		
		arr[i] = data;
	}
}

int Memory::getSize () {
	
	return size;
}

int main(){

	cout << endl;

	Memory a;
	Memory b;
	Memory c;
	Memory d;

	a.loadArray(5);
	b.loadArray(6);
	
	cout << "Dumping c --> c = a + b + a (16)";
	c = a + b + a;
	c.dumpArray();
	
	cout << endl;

	cout << "Dumping d --> d = a (5)";
	d = a;
	d.dumpArray();

	if ( a == b ) cout << "\na is equal to b\n";
	else cout << "\na is NOT equal to b\n";

	//cout << a;
	
	cout << endl;

return 0;
}


cout << a; if that how you intent to use it then it can't be a member function
The message is send to the left object (cout), that's of type ostream.

Either you put it as a member function of ostream, or as a global function
ostream& operator<<( ostream &, const Memory & );
Ok that's how I was going to do it originally but then I reread the assignment and it said member functions so that's how I was trying to do it.. So I guess my next question is how would I output an array in that way? If it was say a point I could just do something like this I think:

ostream &operator<<( ostream &o, const Point &p) {

return ( o << "X: " << p.x << "Y: " << p.y << endl );
}

Would that be right? I am not sure how I would output an array because then the number of fields would vary depending on size of array.
You need 3 parameters: the stream, the array, the array's size.
But the << is a binary operator (2 parameters)

Alternatives:
_ Use a centinel. Like '\0' for c-strings
_ Use a container that knows its size. Like std::vector
_ Use a `print()' function
Topic archived. No new replies allowed.