0

I'm trying to migrate some legacy code into a newer project and I don't really get this one fixed. The code compiled and worked well in the older environment.

I have a header file which contains these definitions:

std::string ToString(shared_ptr<const SomeObject> obj);
std::string ToString(SomeObject* obj);

And an implementation file with following lines:

using namespace std;

string ToString(shared_ptr<const SomeObject> obj)
{
  // code cut
  return outstring.str();
}

string ToString(SomeObject* obj)
{
  // code cut
  return outstring.str();
}

I'm trying to compile it with clang and I get the following redefinition error:

.../Filename.cxx:15:8: error: redefinition of 'ToString' as different
      kind of symbol
string ToString(shared_ptr<const SomeObject> obj)
       ^
.../Filename.h:15:13: note: previous definition is here
std::string ToString(SomeObject* obj);

Why is it a redefinition as different kind of symbol? How should I fix this? And last but not least, why does it work with older compilers?

tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • 1
    Could you provide a minimal test case that reproduces the problem, with all the required includes? – juanchopanza Feb 15 '14 at 12:04
  • Unfortunately not, the framework is humongous :-\ – tamasgal Feb 15 '14 at 12:05
  • Too bad then. Good luck fixing it! – juanchopanza Feb 15 '14 at 12:08
  • 1
    We do not need your framework. Instead we need you to do some work. Take a copy of your framework. Remove stuff -- start by figuring out how to just compile that one file and get the error. Now remove parts of the file you do not think are important to the bug. Recompile and get the same error (if not, roll back -- use source control to make it possible to roll back). Repeat until the code is small and the error remains. – Yakk - Adam Nevraumont Feb 15 '14 at 12:13
  • OK I'll try it. I'm not really experienced in C++ and I hoped that this error is kind of trivial to interpret. – tamasgal Feb 15 '14 at 12:15
  • Maybe someone could explain what the "redefinition of something as different kind of symbol" means? Why does it think I want to redefine it? – tamasgal Feb 15 '14 at 12:19
  • 1
    @septi Are those two the only errors you get? Make sure `string` and `shared_ptr` are declared, and try specifying namespaces for them (replace `shared_ptr` with `boost::shared_ptr` or `std::shared_ptr`). – Anton Feb 15 '14 at 13:03
  • @user3290797 great thanks! Now I also understand the error message ; ) indeed, `shared_ptr` wasn't the same in both files. It works now, thanks again! Could you add an answer, so I can accept it? – tamasgal Feb 15 '14 at 13:12
  • That is one of the reasons to never say `using namespace std;`. – juanchopanza Feb 15 '14 at 13:26

1 Answers1

2

Check if string and shared_ptr are declared, and try specifying namespaces for them (replace shared_ptr with boost::shared_ptr or std::shared_ptr) to make sure that the same class is used in declaration and implementation of ToString.

Anton
  • 3,113
  • 14
  • 12