静态常量类成员的多重定义错误

发布于 2024-11-05 00:15:43 字数 366 浏览 0 评论 0原文

我最近遇到了 奇怪的未定义符号中描述的问题结构/类中的静态常量,我试图通过向所有静态常量类成员(而不仅仅是导致链接器错误的成员)的相应 .cpp 文件中添加定义来使我的代码符合要求。

在多个编译单元中使用常量的情况下,即使定义仅在其中一个编译单元中,我也会收到多个定义错误。

将初始值设定项移至定义可以防止错误,但我宁愿不这样做。

无论如何,虽然我目前在 Visual Studio 中工作,但此代码需要在多个平台上构建。

I recently ran into the problem described in Weird undefined symbols of static constants inside a struct/class and I am trying to bring my code into compliance by adding definitions to the corresponding .cpp files for all of my static const class members, not just the ones that were causing linker errors.

In the cases where the constant is used in multiple compilation units, I am getting multiple definition errors, even though the definition is only in one of the compliation units.

Moving the initializers to the definitions prevent the errors, but I would rather not do that.

For what it's worth, while I am currently working in Visual Studio, this code needs to build on several platforms.

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

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

发布评论

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

评论(4

栀子花开つ 2024-11-12 00:15:43

静态成员变量在类体中声明并在类体外部定义一次。这样做的一般方法是:

class MyClass

{
    static int i;

};

int MyClass::i = 0;

定义在 C++ 源文件中完成,而不是在 header(.h) 中完成。如果这样做,变量将在包含头文件的任何地方定义。看来你也面临着同样的问题。

Static member variables are declared in the class body and defined once outside the class body. The general way of doing this is:

class MyClass

{
    static int i;

};

int MyClass::i = 0;

The definition is done in the C++ source files and not in header(.h). If it is done so, the variable will defined everywhere the header file being included. It seems you are facing this very same problem.

新人笑 2024-11-12 00:15:43

如果启用了语言扩展,Visual Studio 将允许您使用静态 const 对象,而无需在实现文件中定义。不幸的是,当存在显式定义时,它会为正确的 C++ 程序发出错误(如果我没记错的话)。

尝试禁用语言扩展。

If you have language extensions enabled, Visual Studio will allow you to use static const objects without defining the in an implementation file. Unfortunately, it will issue an error (if I remember correctly) for correct C++ programs, when there is an explicit definition.

Try to disable language extensions.

御守 2024-11-12 00:15:43

根据 http://bytes 上的一篇文章。 com/topic/c/answers/710704-const-static-initialization-visual-studio 这实际上可能是一个 Visual Studio 错误,阻止您使用这种形式的初始化。

不幸的是,我认为您可能会被困在源文件中进行初始化以保持可移植性。

我创建了一个简单的示例,可以在 g++ 4.2 中正常编译和链接。

According to one of the posts on http://bytes.com/topic/c/answers/710704-const-static-initialization-visual-studio this may actually be a visual studio bug, preventing you from using that form of initialization.

Unfortunately I think you may be stuck doing the initialization in the source file to maintain portability.

I created a simple example that compiled and linked fine in g++ 4.2.

公布 2024-11-12 00:15:43

我认为如果您希望代码在多个平台上工作,您应该将初始化移至定义(在 .cpp 文件中)。虽然它可能在一个或多个编译器上工作,但不要依赖它的可移植性。

I think if you want your code to work on multiple platforms, you should move the initialisation to the definition (in the .cpp file). While it might work otherwise on one or more compilers, don't rely on it to be portable.

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