Error on overloaded insertion operator

Hello - I'm having problems compiling a C++ program using multiple classes, including an ADT template, within code::blocks. The error message I get is "Reader/stockListType.cpp|228|undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, stockType const&)'|"

- None of my other overloaded operators are throwing errors, not even the extraction operator. I'll post only my code below for both the overloaded insertion and extraction operators in an attempt to streamline the problem. Please let me know if I need to post more. The error is coming from the print function with which I try to use the overloaded insertion operator.

1
2
3
4
// Declaration of overloaded extraction and insertion operators
// stockType header file 
 	friend std::ostream& operator<< (std::ostream& out, const stockType& obj);
	friend std::ifstream& operator>> (std::ifstream& in, stockType& obj);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Definition of overloaded extraction and insertion operators
// stockType implementation file
std::ostream& operator<< (std::ostream& out, stockType& obj)
{
	out << std::fixed << std::showpoint << std::setw(6) << obj.getStockSymbol()
		<< std::setprecision(2) << std::setw(9) << obj.getOpenPrice() << std::setw(10)
		<< obj.getClosePrice() << std::setw(9) << obj.getHighPrice() << std::setw(9)
		<< obj.getLowPrice() << std::setw(10) << obj.getPreviousPrice() << std::setw(10)
		<< obj.getGainLossPercent() << std::setw(9) << obj.getNoShares();

    return out;
}

//Overload the stream extraction operator >>
// stockListType implementation file
std::ifstream& operator>> (std::ifstream& in, stockType& obj)
{
	in >> obj.stockSymbol >> obj.openPrice >> obj.closePrice >> obj.highPrice
		>> obj.lowPrice >> obj.previousPrice >> obj.noShares;

	return in;
}



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
//Calling the overloaded operators 
void stockListType::readIn(std::ifstream& in)
{

	stockType object;

	std::cout << "readIn() was called" << std::endl;

	int x=0;
	while(!in.eof())
	{
		operator>> (in, object);
		stockList.insertAt(object, x);

		x++;
	}

	system("CLS");
	printMenu();


} // end readIn()

void stockListType::printBySymbol()
{
	std::cout << "printBySymbol() was called" << std::endl << std::endl;

	printHeading();

	for(int x=0; x<=stockList.getLength(); x++)
	{
		stockList.setItem(x);
		operator<< (std::cout, stockList.listObject); // <-- ERROR HERE
	}
	printMenu();
}
Line 12 can be written like so: in >> object;
Line 33 can be written like so: std::cout << stockList.listObject;

What type is stockList.listObject?
Why is the extraction operation a friend if it is using public getters?
@ coder777: I knew I should have listed the classes and relationships, but didn't want to overly complicate things.
I rewrote lines 12 and 33 per your suggestion - I'm happy with the improved simplicity but it's still throwing the same error.

Template ADT listType (uses linked lists) - is such a basic class I didn't even have to write any of it.
stockListType derived from listType - handles the reading of a file with stock data in it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public:
	stockListType();
	~stockListType();
	void printMenu();
	void getFileName();
	void readIn(std::ifstream& in);
	void sortStockObjects();
	void sortGainLossIndex();
	double calculateClosingAssets();
	void printHeading();
	void printBySymbol();
	void printByGainLoss();
	void printFooter();

//	stockType getAt(int l);

private:
	double closingAssets;
	listType<stockType> stockList;
	int *sortIndicesGainLoss;


stockType handles everything else, with the overloaded operators and getter and setter functions for public access.

A stockList.listObject then, is a linked list of stock-related values.

**************
@moorecm: I don't really know much about friend functions. In fact, this is the first time I've tried using them. I thought that the point of declaring friend functions was that you could use public getters. Also, this method didn't trip an error on my extraction operator... what would be a better way to call that?
Your prototypes don't match. They should both receive a constant reference

The idea of friends functions is to have access to private members. Considerer it part of the interface of your class.
That way you could get rid of those evil getters.


Also, if a method could be applied to a constant object you need to declare that.
void printHeading() const; //doesn't modify the object
Last edited on
@ ne555: Compiled! Thanks - I removed the constant keyword from the prototype declaration and it's working now. Actually, I originally included the keyword on the function header but received even more errors, so I just did away with it altogether.

Also thanks for the reminder on applying the constant keyword to methods using constant objects. This is still a very rough draft program, but if I finish this up and make it nice and pretty, maybe I get my internship. This program is not the most expeditious way to perform this specific task but my intention is to showcase a variety of C++ topics.
Topic archived. No new replies allowed.