【发布时间】:2020-03-25 15:40:31
【问题描述】:
我需要在多个源文件中使用一个结构,它是我的类的静态成员。这是一个精简的示例:
头文件
namespace NS {
class Foo {
public:
static struct Bar {
bool test = false;
uint32_t value; // uninitialized
} bar;
};
}
源文件 1
#include "myHeader.hpp"
using namespace NS;
Foo::Bar Foo::bar;
/* the rest of my first source file */
在我添加第二个源文件之前,这似乎没有任何问题。
源文件 2
#include "myHeader.hpp"
using namespace NS;
Foo::Bar Foo::bar;
/* the rest of my second source file */
将结构添加到第二个源文件后,我收到“多重定义”错误。有谁知道如何进行这项工作,以便可以在多个源文件中使用静态成员结构?
【问题讨论】:
-
应该只需要在一个文件中定义
Foo::bar。链接器将处理其余部分。 -
在多个源中定义对象是没有意义的。只有一个对象
Foo::bar。只有一个。你想达到什么目的?Does anyone know how to make this work- 让什么工作,究竟是什么?can be used您可以从Source File 2中删除Foo::Bar Foo::bar;并仍然在其中使用Foo::bar。 -
@user4581301 这是我的一个简单的误解。感谢您的澄清。
标签: c++ gcc struct static-members