How can I use a class with the same name from another namespace in my class?

Posted by Beau Simensen on Stack Overflow See other posts from Stack Overflow or by Beau Simensen
Published on 2009-10-14T22:08:23Z Indexed on 2010/04/30 2:07 UTC
Read the original article Hit count: 359

Filed under:
|

I have two classes with the same name in different namespaces. I want one of these classes to reference the other class. The reason is that I am migrating to some newer code and I want to update the old code to simply pass through to the newer code.

Here is a super basic example:

namespace project {
namespace legacy {

class Content {
 public:
  Content(const string& url) : url_(url) { }
  string url() { return url_; }
 private:
  string url_;
};

}} // namespace project::legacy;

namespace project {
namespace current {

class Content {
 public:
  Content(const string& url) : url_(url) {}
  string url() { return url_; }
 private:
  string url_;

}} // namespace project::current;

I expected to be able to do the following to project::legacy::Content, but I am having trouble with some linker issues. Is this an issue with how I'm trying to do this, or do I need to look more closely at my project files to see if I have some sort of weird dependency issues?

#include "project/current/Content.h"
namespace project {
namespace legacy {

class Content {
 public:
  Content(const string& url) : actualContent_(url) { }
  string url() { return actualContent_.url(); }
 private:
  project::current::Content actualContent_;
};

}} // namespace project::legacy;

The test application compiles fine if I try to reference an instance of project::current::Content but if I try to reference project::current::Content from project::legacy::Content I get an:

undefined reference to `project::current::Content::Content(...)`

UPDATE

As it turns out, this was a GNU Autotoolset issue and was unrelated to the actual topic. Thanks to everyone for their help and suggestions!

© Stack Overflow or respective owner

Related posts about c++

Related posts about namespaces