Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to static queue

Tags:

c++

static

I'm new with C++ pthreads. What I'm trying to do is use one thread to catch UDP packets and put it into a queue, and another one to process them and send them after. My question is, how can I push/pop elements into/from a container in a separate thread?

Here's an example:

#include <queue>
#include <iostream>
#include <pthread.h>
#include <signal.h>

class A{
public:
    A(){
        pthread_create(&thread, NULL, &A::pushQueue, NULL);

        pthread_join(thread, NULL);
    }
    virtual ~A(){
        pthread_kill(thread, 0);
    }

private:
    static void* pushQueue(void* context){
        for(int i = 0; i < 10; i++){
            bufferInbound.push(i);
            std::cout << i << " pushed!" << std::endl;
        }
    }

    static std::queue<int> bufferInbound;
    pthread_t thread;
};

int main(){
    A* a = new A();

    return 0;
}

When I compile, it gives me the following result:

U53R@Foo:~/$ make
g++ -g -lpthread main.cpp -c
g++ -g -lpthread main.o -o this
main.o: In function `A::pushQueue(void*)':
/home/U53R/main.cpp:20: undefined reference to `A::bufferInbound'
collect2: ld returned 1 exit status
make: *** [make] Error 1

Thanks for helping.

like image 206
Vargeux Avatar asked Dec 16 '10 23:12

Vargeux


1 Answers

you need to initialize the static member, add std::queue<int> A::bufferInbound; after the class or move it inside your function.

like image 133
OneOfOne Avatar answered Oct 21 '22 05:10

OneOfOne