So I have in stockType.h
#include <iostream>
class stockType {
public:
//...
static friend std::ostream& operator<<(std::ostream& out, const stockType& stock);
static friend std::istream& operator>>(std::istream& in, stockType& stock);
private:
//...
}
and in stockType.cpp
std::ostream& operator<<(std::ostream& out, const stockType& stock) {
//...
}
std::istream& operator>>(std::istream& in, stockType& stock) {
//...
}
I'm having no issues with operator<<
but the compiler gives me a fatal error "static function 'std::istream &operator >>(std::istream &,stockType &)' declared but not defined
" and it says it occurs on main.cpp line 32 but there's only 31 lines in main.cpp.
CodePudding user response:
Marking a free function (friend
functions aren't methods in a class) as static
indicates that it will be defined in the current translation unit. Putting a static
function in a header means that all translation units including that header must have their own definition of that function. You should remove static
from your declarations (clang and gcc actually reject your code as invalid).
Note that this is different to static
member functions which indicate that the method is available in the class rather than in instances. This is one of the many confusing places that c uses the same keyword to mean different things in different contexts.