共享通用对象 - 警告“已定义但未使用”

发布于 2024-12-11 08:03:54 字数 405 浏览 0 评论 0原文

我有许多 C++ 类,其中很多(不是全部)共享两个“静态大小变量”,例如

share.h

/*Other variables in this header used by all classes*/ 

static size width=10;//Used by about 60% 

static size height = 12;//used by about 60% 

所以我将它们与所有类共享的其他对象一起放在头文件中。

当我编译项目时,我收到很多警告(来自不使用这些警告的类),这些警告抱怨它们被定义但未被使用。但我需要他们在那里!

所以我问,有没有办法定义它们,以便不使用这两个变量的类可以使用这个头文件,而不会抛出有关它们未定义的警告?

先感谢您

I have a number of C++ classes, alot of them (not all) share two "static size variables" e.g.

share.h

/*Other variables in this header used by all classes*/ 

static size width=10;//Used by about 60% 

static size height = 12;//used by about 60% 

So I placed them in a header file along with other objects that all classes share.

when I compile the project I get alot of warnings (from the classes which dont use these), which complain about them being defined and not used. But I need them there!

So I ask, is there a way to define these, so that classes not using these two variables can use this header file without throwing warnings about them not being defined?

Thank you in advance

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

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

发布评论

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

评论(3

七颜 2024-12-18 08:03:54

要么声明它们 const,要么声明它们 extern 并在一个源文件中定义它们。编译器应该期望常量被定义(在头文件中)但不被使用,并且不会给出警告。

定义不使用的静态变量通常是错误的标志,因此警告在这种情况下很有用。 (如果您确实希望在多个翻译单元中获得这些变量的单独的、可修改的副本,那么您可能应该重新考虑您的程序设计)。

Either declare them const, or declare them extern and define them in exactly one source file. A compiler should expect constants to be defined (in header files) but not used, and not give a warning for that.

Defining static variables that you don't use is often a sign of an error, so the warning is useful in that case. (If you actually do want separate, modifiable copies of these variables in multiple translation units, then you should probably rethink your program design).

可是我不能没有你 2024-12-18 08:03:54

该警告并没有说它们没有被定义。该警告特别指出它们正在被定义,但尚未使用。也就是说,你要求了一些东西,但你没有使用它。

您真正的问题是这些是静态全局变量。这意味着包含标头的每个 .c 或 .cpp 文件都将拥有该变量的自己的副本

因此,如果您有包含 share.ha.cppb.cpp,则 a.cpp 可以更改width 更改为 20,但 b.cpp 无法看到该更改的效果。这就是编译器发出警告的原因。因为如果您在文件中声明静态大小宽度,并且不在该特定文件中使用它,那么您肯定不能在任何地方使用它否则。定义一个从未使用过的变量是可疑的行为。

一般来说,您不应该将全局静态变量放入标头中。如果你想要常量值,你应该这样声明它们:

const size width=10;//Used by about 60% 
const size height = 12;//Used by about 60% 

如果你不希望它们是常量,那么你应该在标头中使用 extern 声明它们(不初始化它们),然后选择 < em>one .cpp 文件并定义它们(不带 extern,但带有一个值)。如下:

//share.h
extern size width;
extern size height;

//share.cpp
size width = 10;
size height = 12;

The warning doesn't say that they aren't being defined. The warning specifically says that they are being defined, but not used. That is, you asked for something, but then you didn't use it.

Your real problem is the fact that these are static global variables. This means that each .c or .cpp file that includes the header will have it's own copy of that variable.

So if you have a.cpp and b.cpp that include share.h, a.cpp can change width to 20, but b.cpp cannot see the effect of that change. That's why the compiler issues a warning. Because if you declare a static size width in a file, and don't use it in that particular file, then you certainly can't be using it anywhere else. And that's suspicious behavior, to define a variable that you never use.

In general, you should never put global static variables in a header. If you want constant values, you should declare them as such:

const size width=10;//Used by about 60% 
const size height = 12;//Used by about 60% 

If you don't want them to be constant, then you should declare them in a header with extern (without initializing them), then pick one .cpp file and define them (without extern, but with a value). As follows:

//share.h
extern size width;
extern size height;

//share.cpp
size width = 10;
size height = 12;
感情废物 2024-12-18 08:03:54

您需要使用 const 限定符,而不是 static。 static 的含义与你的意图完全不同。您可以在此处找到有关静态的更多信息 http://www.cprogramming.com/tutorial/statickeyword.html

You need to use the const qualifier, not static. The meaning of static is totally different from your intent. You can find more information about static here http://www.cprogramming.com/tutorial/statickeyword.html.

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