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

发布于 2024-08-18 05:19:07 字数 710 浏览 8 评论 0原文

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

我的单元测试框架 (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)
 {
  ...
 }

然而,我最近发现(至少在 VS2005 中),当您使用相同的名称命名固定结构时(因此现在存在具有相同名称的两个版本的结构),那么其中一个版本会被默默地抛出< /强>。这非常令人惊讶,因为我将编译器设置为 /W4(最高警告级别)并且没有发出任何警告。我想这是一个名称冲突,以及为什么发明了命名空间,但是我真的需要将每个单元测试装置包装在单独的命名空间中吗?我只是想确保我没有遗漏一些更基本的东西。

有没有更好的方法来解决这个问题——这种情况应该发生吗?我不应该看到重复符号错误或其他错误吗?

Somewhat of an academic question, but I ran into this while writing some unit tests.

My unit test framework (UnitTest++) allows you to create structs to serve as fixtures. Usually these are customized to the tests in the file, so I put them at the top of my unit test file.

//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)
 {
  ...
 }

However, I found recently (with VS2005 at least) that when you name the fixture struct using the same name (so now two versions of the struct exist with the same name), then one of the versions is silently thrown out. This is pretty surprising, because I have my compiler set to /W4 (highest warning level) and no warning comes out. I guess this is a name clash, and why namespaces were invented, but do I really need to wrap each of my unit test fixtures in a separate namespace? I just want to make sure I'm not missing something more fundamental.

Is there a better way to fix this - should this be happening? Shouldn't I be seeing a duplicate symbols error or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-08-25 05:19:07

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

无法访问 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)
{
 ...
}

Try sticking the classes in an anonymous namespace, you may find it less distasteful than having to create and name a new namespace for each file.

Don't have access to VS2005 and Cpp unit but this may work..

//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)
{
 ...
}
三人与歌 2024-08-25 05:19:07

编译器一次只能在一个编译单元上工作;这将是源文件及其#include 的任何内容。由于您的类位于不同的文件中,因此不会发生冲突。

链接器将所有内容放在一起,但它不知道类定义,因此也看不到冲突。

回到 C 时代,链接器很常见地认识到您有两个具有相同名称的不同函数并生成错误消息。对于 C++ 中的内联函数和模板,它不能再这样做了 - 不同的编译单元通常会包含同一函数的重复项,因此链接器只是假设它们是相同的。

The compiler only works on a single compilation unit at a time; this would be the source file and anything it #includes. Since your classes are in different files, no conflict there.

The linker puts everything together, but it doesn't know about class definitions so it doesn't see a conflict either.

Back in the days of C, it was quite common for the linker to recognize that you had two different functions with the same name and generate an error message. With inline functions and templates in C++, it can't do that anymore - different compilation units will often contain duplicates of the same function, so the linker just assumes they're the same.

迷途知返 2024-08-25 05:19:07

这基本上是因为类需要在头文件中定义,这导致每个目标文件中都有冗余的类定义。因此,可以处理 C++ 链接的链接器必须将多余的类声明折叠在一起,并假装该类仅声明一次。

链接器没有任何方法可以区分包含在多个对象中的单个类和多个对象中具有相同名称的多个类。

您必须使用名称空间(或更好的语言)来解决这个问题。

This is basically a consequence of the fact that classes need to be defined in header files, which leads to having redundant definitions of the class in each object file. So a linker that can handle C++ linkage has to fold redundant class declarations together and pretend that the class was declared only once.

There isn't any way for the linker to distinguish between a single class included into multiple objects and multiple classes with the same name in multiple objects.

You have to use namespaces (or a better language), to get around this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文