C++/VS2005:在两个不同的.cpp文件中定义相同的类名

Joe*_*der 3 c++ namespaces visual-studio-2005 name-clash

有些学术问题,但我在编写一些单元测试时遇到了这个问题.

我的单元测试框架(UnitTest ++)允许您创建结构以用作夹具.通常这些都是根据文件中的测试自定义的,所以我将它们放在单元测试文件的顶部.

//Tests1.cpp

struct MyFixture {  MyFixture() { ... do some setup things ...} };

TEST_FIXTURE(MyFixture, SomeTest)
{
  ...
} 

//Tests2.cpp

struct MyFixture { MyFixture() { ... do some other setup things, different from Tests1}};

 TEST_FIXTURE(MyFixture, SomeOtherTest)
 {
  ...
 }
Run Code Online (Sandbox Code Playgroud)

但是,我最近发现(至少使用VS2005),当你使用相同的名称命名fixture结构时(现在结构的两个版本存在同名),然后静默抛出其中一个版本.这是非常令人惊讶的,因为我将我的编译器设置为/ W4(最高警告级别)并且没有出现警告.我想这是一个名称冲突,为什么命名空间被发明,但我真的需要将每个单元测试装置包装在一个单独的命名空间中吗?我只是想确保我没有错过更基本的东西.

有没有更好的方法来解决这个问题 - 这应该发生吗?我不应该看到重复的符号错误或什么?

Mic*_*son 8

尝试将类粘贴在匿名命名空间中,您可能会发现它比为每个文件创建和命名新命名空间更不令人厌恶.

无法访问VS2005和Cpp单元,但这可能有效..

//Tests1.cpp
namespace
{
struct MyFixture {  MyFixture() { ... do some setup things ...} };
}

TEST_FIXTURE(MyFixture, SomeTest)
{
  ...
} 


//Tests2.cpp
namespace
{
struct MyFixture { MyFixture() { ... do some other setup things, different from Tests1}};
}

TEST_FIXTURE(MyFixture, SomeOtherTest)
{
 ...
}
Run Code Online (Sandbox Code Playgroud)