简体   繁体   English

在.cpp文件中定义需要访问私有结构的静态成员

[英]Defining a static member in .cpp file which requires access to private struct

So to declare a static member of a class, a definition of that member is required in a .cpp file to avoid an unresolved external linker error. 因此,要声明类的静态成员,必须在.cpp文件中对该成员进行定义,以避免未解决的外部链接器错误。 My problem is that my static member requires the definition of a private struct which won't be available to my static member in the .cpp file. 我的问题是我的静态成员需要定义一个私有结构,该私有结构对.cpp文件中的我的静态成员不可用。

    //foo.h
    class A
    {
    public:
        ...
    private:
        struct B
        {
            ...
        };

        class C
        {
        public:
            ...
        private:
            static std::vector<std::shared_ptr<B>> someVector;
        } D;
    };

You should declare the vector in the cpp file like this: 您应该在cpp文件中声明矢量,如下所示:

std::vector<std::shared_ptr<A::B>> A::C::someVector;

struct B is unknown outside class A so it must be reffered on the global scope as A::B struct Bclass A之外是未知class A因此必须在全局范围内将其引用为A::B

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

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