在同一类的方法中使用类的静态成员变量
using static member variable of a class inside a method of this same class
如果我在文件Af.h中声明了a类的静态成员变量我想在文件Af.cpp中的同一类的方法"met"中使用这个变量,我该如何处理?这是我的文件
Af.h
class A
{
public:
static std::vector <int> vec;
void met();
//....
};
Af.cpp
//...
void A::met()
{
// I will use here some int variable i
vec.push_back(i);
//...
}
不幸的是,此代码提供了以下编译错误:
undefined reference to A::vec
您需要在Af.cpp
:中定义它
std::vector<int> A::vec;
相关文章:
- C++:是否可以使用非静态成员变量模板?
- 如何在复杂继承中访问静态成员变量
- 静态成员变量不会由 gettext 转换
- 未使用的C++未优化的静态成员函数/变量
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- C++ lambda - 捕获静态成员变量
- 多线程处理中的静态成员变量
- 纯标头库中静态成员变量的正确设置器和 getter
- 如何从具有专用化的类模板定义静态成员变量?
- 从静态成员函数访问私有非静态类变量 - C++
- 静态变量与静态成员
- 为什么可以在没有实例变量的情况下访问静态回调方法中的静态成员变量?
- 声明和定义类静态成员变量不会导致多重声明,这是为什么?
- 为什么不允许静态成员变量的初始化在类中,而允许静态静态成员的初始化
- 类中的静态成员变量和C++中的全局变量有什么区别?
- 获取 constexpr 全局变量(不是静态成员)的链接器符号
- C++ 静态成员变量 - 文件之间的访问
- 将非静态成员函数C 的使用无效,而将成员功能作为函数变量
- 如何在运行时使用静态成员函数初始化静态成员变量
- 如何在类静态成员中引用静态变量?