简体   繁体   中英

dispatch_queue_t as instance variable in C++ class

I am trying to write a wrapper around (GCD) queue.

I have it like this in header file

#include <dispatch/dispatch.h>

namespace myspace {

class Queue 
{

public: 
    Queue(dispatch_queue_t dispatchQueue);
private:
    dispatch_queue_t dispatchQueue_;
}
}

The counterpart of header is .mm file with implementation.

I import Queue.h into some other pure C++ file and when linking I get:

Undefined symbols for architecture armv7:
   "myspace::Queue::Queue(dispatch_queue_s*)", referenced from:
        myspace::NiceClass::CreateOneMoreQueue() in NiceClass.o 

In NiceClass I create a new Queue:

Queue *queue = new Queue(dispatch_get_main_queue());

I'm stuck with this one. It seems like I can't do it like this. I could change dispatch_queue_t to void* and it works like that but I lose type information and get lots of casts.

I would appreciate any idea.

EDIT: this is implementation in Queue.mm. Memory management and other details omitted.

Queue::Queue(dispatch_queue_t dispatchQueue) 
{
    dispatchQueue_ = dispatchQueue;
    if (dispatchQueue_ == NULL) {
        dispatchQueue_ = dispatch_get_main_queue();
    }
}

void Queue::AddMessage(Handler *handler, Context *context)
{
    Message *msg = new Message;
    msg->phandler = handler;
    msg->context = context;
    dispatch_async(dispatchQueue_, ^{
        handler->ProcessMessage(msg);
    });
}

EDIT2: Here is a test project which fails for me zip 32k

Wow! 5 years ago! For anyone who stumbles across this now!

If myspace is a 'cpp'(Pure C++) file and Queue is a 'mm'(ObjC++) file, you will have linker failures across the language barrier when passing "dispatch" objects as they are NSObjects in ObjC++.

Change both sides to '.mm' and it should resolve the linker issues.

add these lines:

namespace myspace {
....
}

around the implementation of

Queue::....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM