简体   繁体   English

C ++定义静态成员的正确方法是什么

[英]C++ what's the correct way to define static member

I'm trying to use a static member in a normal member function. 我正在尝试在普通成员函数中使用静态成员。 But the compiler report some errors. 但是编译器报告一些错误。 Pls take a look at this code 请看一下这段代码

#include <memory>

template<typename T>
class MyClass {
private:
  static std::allocator<T> alloc;
  T *p;
public:
  void assign(T e) {
    p = alloc.allocate(1);
    alloc.construct(p, e);
  }
};

and this is how I use it: 这就是我的用法:

#include 'myclass.h'

int main() {
  MyClass<int> cls;
  cls.assign(4);

};

And the compiler give this error: 并且编译器给出此错误:

/Users/liuziqi/CLionProjects/cpplearning/src/tt.h:17:9: warning: instantiation of variable 'MyClass<int>::alloc' required here, but no definition is available [-Wundefined-var-template]
    p = alloc.allocate(1);
        ^
/Users/liuziqi/CLionProjects/cpplearning/src/main.cpp:49:7: note: in instantiation of member function 'MyClass<int>::assign' requested here
  cls.assign(4);
      ^
/Users/liuziqi/CLionProjects/cpplearning/src/tt.h:13:28: note: forward declaration of template entity is here
  static std::allocator<T> alloc;
                           ^
/Users/liuziqi/CLionProjects/cpplearning/src/tt.h:17:9: note: add an explicit instantiation declaration to suppress this warning if 'MyClass<int>::alloc' is explicitly instantiated in another translation unit
    p = alloc.allocate(1);

I can not figure out which part is wrong.....I have defined that static member and any member function should be able to use it. 我不知道哪一部分是错误的.....我已经定义了静态成员和任何成员函数都应该能够使用它。 Is this error relevant to template ? 此错误与模板有关吗? (I've just learned template and not sure If I use it correctly.) (我刚刚学习了模板,不确定是否正确使用它。)

I'm going to take a shot at describing what the warning is referring to in addition to answering the "how to fix it" question (which certainly has been answered many times before)... 除了回答“如何解决”问题(肯定已经多次回答)之外,我还将介绍警告所指的内容。

Because MyClass is a template, the compiler expects all the code for the templated class to be available in the same file ( myclass.h ). 因为MyClass是模板,所以编译器希望模板化类的所有代码在同一文件( myclass.h )中都可用。 Since you only declare but do not define alloc in myclass.h , the compiler assumes you made a mistake. 由于您仅在myclass.h声明但未定义alloc,因此编译器认为您犯了一个错误。 It's not absolutely required to be there (hence the warning and not error ) and you could disable the warning if you define the variable elsewhere, but it's almost certainly just a mistake. 它不是绝对必需的(因此警告而不是error ),如果在其他位置定义变量,则可以禁用警告,但是几乎可以肯定这只是一个错误。

If you're using c++17, the easiest way to deal with this is to declare the static member as inline, so it will be defined right there: 如果您使用的是c ++ 17,处理此问题的最简单方法是将静态成员声明为内联,因此将在此处定义它:

  static inline std::allocator<T> alloc;

live: https://godbolt.org/g/cr1aUP 直播: https//godbolt.org/g/cr1aUP

Prior to C++17, you would explicitly define the variable in myclass.h : 在C ++ 17之前,您将在myclass.h明确定义变量:

template<typename T>
std::allocator<T> MyClass<T>::alloc;

live: https://godbolt.org/g/2Znqst 直播: https//godbolt.org/g/2Znqst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM