Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ multiple definition of operator>>

I'm using the proposed solution by @Martin for csv parsing with C++, as I'm trying to avoid using libraries like boost and such for my current project. I've placed his implementation in a "csv.h" header and am trying to include it with some other files. I keep getting the following error

multiple definition of operator>>(std::basic_istream<char, std::char_traits<char> >&, CSVRow&)

when I try to build the project - I'm assuming this happens because the redefinition of operator>> clashes with the original one. How can I make these two play nice? thanks.

like image 279
sa125 Avatar asked Apr 18 '11 12:04

sa125


1 Answers

Chances are you have the same operator included in multiple compilation units (ie cpp files) so you're getting the same code generated, the linker than looks at all the .obj files to pull them together and sees multiples.

You have 3 choices:

  • mark it as static - this will make the operator visible only to the file it was in.
  • mark it inline - this gets rid of the function and inserts the code at the point of use.
  • Put the prototype in the header and the body in its own cpp file.
like image 127
gbjbaanb Avatar answered Sep 28 '22 15:09

gbjbaanb