简体   繁体   中英

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. 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.

    //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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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