0

This is a MSVP of a problem I am facing. What is wrong with m_eventQueue being static in the code below ? When it is not static, it compiles fine. I want it to be static because I am planning to use it in another class as well and it is a common queue between them.

This is the error I get

badri@badri-All-Series:~/progs$ g++ --std=c++11 inher3.cpp 
/tmp/ccGTVi2d.o: In function `RecordingConfigJobStateSignal::RecordingConfigJobStateSignal(std::shared_ptr<EventQueue> const&)':
inher3.cpp:(.text+0x13c): undefined reference to `commonQueue::m_eventQueue'
collect2: error: ld returned 1 exit status

This is inher3.hpp

#include<iostream>
#include<memory>
#include<queue>
using namespace std;
class EventBase
{
public:

private:
        int a;
};
using EventBasePtr = std::shared_ptr<EventBase>;

class SubscriptionManager
{

        public:
                int x;
};

class EventQueue
{
public:
    explicit EventQueue( SubscriptionManager& ){};
    ~EventQueue();
private:
    std::queue< EventBasePtr >          m_queue;
};
using EventQueuePtr = std::shared_ptr<EventQueue>;


class commonQueue
{
        public:             
                int *a;
                static std::queue< EventBasePtr >       m_queue;
                static EventQueuePtr m_eventQueue;
};

class RecordingConfigJobStateSignal: public commonQueue
{
        public:
                int c;
                RecordingConfigJobStateSignal( const EventQueuePtr &);
        private:
                int b;

};

This is inher3.cpp

#include<iostream>
#include"inher3.hpp"

RecordingConfigJobStateSignal::RecordingConfigJobStateSignal( const EventQueuePtr& eventQueue )//:m_eventQueue( eventQueue )
{
        /* m_eventQueue is actually from class commonQueue */
        m_eventQueue = eventQueue;
}

int main()
{
        return 0;
}


badri
  • 575
  • 2
  • 8
  • 22
  • 1
    You only *declare* the static member variables in the `commonQueue` class. You also need to *define* them. [A decent book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), tutorial or class should have the information you need. – Some programmer dude Dec 01 '21 at 07:36

1 Answers1

1

When you declare a static member in a class, C++ requires you to define the member outside of the class explicitly. Put this outside of your class in the .cpp file:

/*static*/
EventQueuePtr commonQueue::m_eventQueue;
D-FENS
  • 1,438
  • 8
  • 21
  • One more thing, you can treat the static member just as a global variable preceded by "commonQueue::". More specifically, you need to link it like a global variable when you link your code as a library, it needs to be exported as a symbol. – D-FENS Dec 01 '21 at 07:55