未定义的静态队列引用

Var*_*eux 5 c++ static

我是C++ pthreads的新手.我正在尝试做的是使用一个线程来捕获UDP数据包并将其放入队列,另一个线程用于处理它们并在之后发送它们.我的问题是,如何在单独的线程中将元素推入/移出容器?

这是一个例子:

#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;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,它给了我以下结果:

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
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

One*_*One 9

你需要初始化静态成员,std::queue<int> A::bufferInbound;在类之后添加或在函数内移动它.

  • 对.此外,这个问题与线程无关.(虽然OP需要一些锁定.) (3认同)