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
  • 919
  • 1
  • 8
  • 16
  • 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
  • 505,946
  • 29
  • 409
  • 696
robthebloke
  • 7,832
  • 6
  • 11