tags:

views:

193

answers:

3

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!

A: 

Have you tried ::project::current::Content ( notice :: at the start of the reference ).

This is similar to /path/to/file and path/to/file when locating file in the file system.

Alexander Pogrebnyak
I just tried that. It did not seem to make a difference. Thanks for the suggestion!
Beau Simensen
A: 

If the source files have the same name, they will create object files with the same name (at least in Visual C++ 2008). This will cause a problem as one of the object files will overwrite the other and linking will fail. Make sure that you either rename one of the files or more appropriately, rename one of the object files.

In order to rename one of the object files in Visual C++ 2008, right click on the source file, and select properties. Navigate to C/C++ -> Output Files. Change the Object File Name from $(IntDir)\ to $(IntDir)\$(InputName)1.obj.

Hope this helps.

szielenski
A: 

You have }; missing in the first example, in project / legacy definition.

In Visual Studio 2008, the project with the following configuration:

current.h:

#include "stdafx.h"
#include <string>
using namespace std;

namespace project {
namespace current {

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

};
}
} // namespace project::current;

namespaces.cpp:

#include "stdafx.h"
#include <string>

#include "current.h"

using namespace std;

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;



int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

builds without problems.

Ashalynd