具有对静态成员的未定义引用是什么意思? [英] What does it mean to have an undefined reference to a static member?

查看:134
本文介绍了具有对静态成员的未定义引用是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了一个有一些静态数据成员的类,但现在我收到关于未定义的引用的错误。为什么这不工作?

I just wrote a class with some static data members, but now I am getting errors about "undefined references". Why doesn't this work? What am I doing wrong?


(注意:这是一个输入 Stack Overflow的C ++常见问题如果你想批判在这种形式提供FAQ的想法,那么在meta上发布所有这一切将是这样做的地方。这个问题的答案是在 C ++聊天室中进行监控,其中常见问题解答首先开始,因此您的答案非常

(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)

推荐答案

要了解这一点,您应该对编译和链接,以及声明和定义之间的差异

To understand this, you should have a good understanding of compiling and linking, and the differences between declarations and definitions.

考虑下面的类:

//In header file
class Example {
    static bool exampleStaticMember;
};

这里, exampleStaticMember 。这意味着如果 exampleStaticMember 的使用方式意味着它必须有一个地址,那么它必须有一个单独的定义。一般来说,在类定义中没有静态数据成员的声明是该成员的定义。

Here, exampleStaticMember is declared but not defined. This means that if exampleStaticMember is used in a way that means that it must have an address then there must be a separate definition for it. In general, no declaration of a static data member in a class definition is a definition of that member.

所需声明通常放在cpp文件中,类的成员的定义。它必须与类定义位于相同的命名空间中。该定义通常如下:

The required declaration is usually put in the cpp file which contains the other definitions for the members of the class. It must be in the same namespace as the class definition. The definition typically looks like:

//In source file:
//This may optionally have an initialiser (eg "= true")
bool Example::exampleStaticMember; 

定义可以放在任何cpp文件中,但不能放在标题中类,因为这很可能会打破一种定义规则

The definition can be put in any cpp file, but it should not be put in the header with the class, because that would be likely to break the One Definition Rule.

作为一种特殊情况,如果静态成员变量是一个const整数或枚举类型,那么它可以在类定义中有一个初始化器:

As a special case, if the static member variable is an const integral or enumeration type then it can have an initialiser in the class definition:

//In header file
class Example {
    static const int initialised = 15;
};

在这种情况下,cpp文件中的定义仍然是必需的,一个初始化程序:

In this case, the definition in the cpp file is still required, but it is not allowed to have an initialiser:

//In source file
//Note: no initialiser!
const int Example::initialised;

这样初始化的静态成员可以在常量表达式中使用。

Static members that have been initialised like this can be used in constant expressions.

模板

对于模板的静态数据成员,事情稍有不同。静态成员应该在标题中与类的其余部分一起定义:

For a static data member of a template, things are slightly different. The static member should be defined in the header along with the rest of the class:

//In header file
template<typename T>
class Example {
    static int exampleInt;
    static T exampleT;
}
template<typename T> int Example<T>::exampleInt;
template<typename T> T Example<T>::exampleT;

这样做是因为类模板的静态数据成员的单一定义规则有一个特定的异常。

This works because there is a specific exception to the One Definition Rule for static data members of class templates.

静态的其他用途

static 关键字应用于不在类范围中的函数和对象,它可以具有非常不同的含义。

When the static keyword is applied to functions and objects that are not in a class scope it can take on a very different meaning.

一个函数范围,它声明一个对象,在函数的第一次执行中初始化,并随后在函数调用之间保持其值。

When applied to objects in a function scope, it declares an object that is initialised in the first execution of the function and that subsequently keeps its value between function calls.

当应用于对象或函数时命名空间范围(任何类或函数定义之外),它声明对象或函数与内部链接。此对象已弃用于对象,因为未命名命名空间提供了一个更好的替代方案。

When applied to objects or functions at namespace scope (outside of any class or function definition), it declares objects or functions with internal linkage. This usage is deprecated for objects, as the unnamed-namespace provides a better alternative.

这篇关于具有对静态成员的未定义引用是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆