0

source.h:

#include <iostream>
class date{
public:
std::string str_time;
friend std::istream& operator >> (std::istream& para_stream, date& para_date);
};

source.cpp:

#include "source.h"
std::istream& operator >> (std::istream& para_stream, date& para_date)
{
  istream >> para_date.str_time;
  return istream;
}

ERROR: Error 2 error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits > & __cdecl src::operator>>(class std::basic_istream<char,struct std::char_traits > &,class src::date &)" (??5src@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVdate@0@@Z) referenced in function "public: bool __thiscall src::DB::del_vouc(int const &)" (?del_vouc@DB@src@@QAE_NABH@Z) C:\Users\Dell\Documents\Visual Studio 2013\Projects\foodstore\foodstore\DB.obj foodstore

Tris Yang
  • 15
  • 7
  • You forgot to link with the definition. It has nothing to do with overloading, operators, or `istream`. – molbdnilo May 18 '21 at 11:40
  • The error is talking about a DB object file - how are you building your stuff? – doctorlove May 18 '21 at 11:41
  • Does this answer your question? [LNK2019 error c++ unresolved external symbol](https://stackoverflow.com/questions/5730135/lnk2019-error-c-unresolved-external-symbol) – anatolyg May 18 '21 at 11:41
  • @doctorlove yes, I know that as well, but when I remove this operator overloaded functions, it doesn't give error. – Tris Yang May 18 '21 at 11:47
  • You really should read the post from anatolyg's comment. The `include` keyword only loads the include file at compile time. But to build an executable you have to link `source.o` with you main source. – Serge Ballesta May 18 '21 at 11:49
  • Link is about libraries; include is about compiling – doctorlove May 18 '21 at 11:49
  • @doctorlove DB file is where I exacute the overloaded operator function. – Tris Yang May 18 '21 at 11:49
  • Maybe because you have defined it in the source.cpp file, but not told the world about it in the header, it's not visible outside the translation unit? – doctorlove May 18 '21 at 11:50
  • @doctorlove "told the world about it in the header", sorry, I don't quite understand what that means, How can I do that? – Tris Yang May 18 '21 at 11:53
  • You gave a function in a cpp file. If you declare it in the header, anything including the header can use it. If you don't declare it in the header it stays "hidden" in the source file – doctorlove May 18 '21 at 11:58
  • @doctorlove so, I should put the overloaded function in header file? – Tris Yang May 18 '21 at 12:01
  • You'd need to mark it inline then; alternatively just put the declaration, `std::istream& operator >> (std::istream& para_stream, date& para_date);` and leave the definition in the cpp file – doctorlove May 18 '21 at 13:04
  • @doctorlove wow, it worked. Thanks! But why do I need to put inline? can you explain? – Tris Yang May 18 '21 at 13:26
  • Going to add an answer cos these comments got out of hand – doctorlove May 18 '21 at 13:30
  • Hope the answer makes sense – doctorlove May 18 '21 at 13:34

2 Answers2

0
istream >> para_date.str_time;
return istream;

These two lines should be as :

para_stream >> para_date.str_time;  // para_stream is the object of class istream 
return para_stream ;

You should use the object of the class para_stream, not the class. After this change,it compiled successfully in my system.

enter image description here

enter image description here

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
0

The linker is complaining about src::DB having an "unresolved external symbol"

You have defined a function inside source.cpp, so it remains internal to this file (translation unit).

If you add a declaration to the header file, like this

std::istream& operator >> (std::istream& para_stream, date& para_date);

it becomes available externally, rather than "hidden" inside the cpp file.

Alternatively, you can define (rather than just declare) it in the header file, but it MUST be marked inline otherwise you end up with a definition every time the header is included.

doctorlove
  • 18,872
  • 2
  • 46
  • 62