Can you use a method inside a stream insertion overload?

I'm trying to overload << to print some things. Creating a simple print() method works fine like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// stockTypeImp.cpp implementation file
#include <iostream>
#include <iomanip>
#include <string>
#include "stockType.h"

using namespace std;

void print()
{
	cout << setw(6) << symbol;
	cout << setw(9) << open;
	cout << setw(9) << close;
	osObject << setw(9) << fixed << showpoint << setprecision(2) << getGain() << "%";
}
double stockType::getGain()
{
	double gain = ((close - open)/open)*100;
	return gain;
}


However, when I replace the above print() function with the << overload:

1
2
3
4
5
6
7
8
9
ostream& operator<< (ostream& osObject, const stockType& stock)
{
	osObject << setw(6) << stock.symbol;
	osObject << setw(9) << stock.open;
	osObject << setw(9) << stock.low;
	osObject << setw(9) << fixed << showpoint << setprecision(2) << stock.getGain() << "%";

	return osObject;
}


I get an error that says: stocktypeimp.cpp(37): error C2662: 'stockType::getGain' : cannot convert 'this' pointer from 'const stockType' to 'stockType &'
Error: Object has type qualifiers that are not compatible with the member function.

Is there a way I can use getGain() inside the << overload?

Thanks

For reference:
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
//stockType.c 

#ifndef H_myArray
#define H_myArray

using namespace std;

class stockType
{
	friend ostream& operator<< (ostream&, const stockType&);

public:
	void setStock(string sym, double op, double cl, double hi, double lo, double pre, int vol);

	//void print();

	double getGain();

	void printGain();

	stockType(string sym, double op, double cl, double hi, double lo, double pre, int vol);
	stockType();

private:
	string symbol;
	double open;
	double close;
	double high;
	double low;
	double previous;
	int volume;
};

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
//stockTypeImp.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "stockType.h"

using namespace std;

void stockType::setStock(string sym, double op, double cl, double hi, double lo, double pre, int vol)
{
	symbol = sym;
	open = op;
	close = cl;
	high = hi;
	low = lo;
	previous = pre;
	volume = vol;
}

ostream& operator<< (ostream& osObject, const stockType& stock)
{
	osObject << setw(6) << stock.symbol;
	osObject << setw(9) << stock.open;
	osObject << setw(9) << stock.close;
	osObject << setw(9) << stock.high;
	osObject << setw(9) << stock.low;
	osObject << setw(9) << stock.previous;
	osObject << setw(9) << fixed << showpoint << setprecision(2) << stock.getGain() << "%";
	osObject << setw(16) << stock.volume;

	return osObject;
}
The error isn't specific to insertion operators or overloading; it's because you're passing your stocker to operator<< by const ref, but your accessor is non-const. Change

1
2
3
double stockType::getGain()
{
    ....


to

1
2
3
double stockType::getGain() const
{
    ....


with the corresponding change in the class declaration.

Andy
Thanks Andy.
Topic archived. No new replies allowed.