0

I have a class that has a static member struct

class SharedMem
{
public:     

    struct memory { 
        char buff[100]; 
        int status, pid1, pid2; 
    }; 

    static struct memory* shmptr; 
}

I would like to define the static struct using SharedMem::memory shmptr;

But I'm getting errors undefined reference to 'SharedMem::shmptr'

How do I properly define the struct in C++?

And follow up question, how can I define this struct if my class is entirely in the header file, can I define it after the class declaration at the bottom of the header file?

Thanks

Mi Po
  • 1,123
  • 2
  • 12
  • 21
  • Does this answer your question? [Undefined reference to static variable c++](https://stackoverflow.com/questions/16284629/undefined-reference-to-static-variable-c) – Ruzihm Jan 13 '20 at 23:07
  • And [here](https://stackoverflow.com/questions/18860895/how-to-initialize-static-members-in-the-header) answers your second question. – Douglas Oliveira Jan 13 '20 at 23:13

1 Answers1

5
class SharedMem
{
public:     

    struct memory { 
        char buff[100]; 
        int status, pid1, pid2; 
    }; 

    static memory* shmptr; 
};
// must add this in the cpp file!
SharedMem::memory* SharedMem::shmptr = nullptr; 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
robthebloke
  • 9,331
  • 9
  • 12