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);
@ 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
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
@ 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.