C++ 类型为'的参数;无效*(…)(无效*)&x27;不匹配';无效*(*)(无效*)

C++ 类型为'的参数;无效*(…)(无效*)&x27;不匹配';无效*(*)(无效*),c++,multithreading,void,gnuradio,C++,Multithreading,Void,Gnuradio,我目前正在GNU电台上开发一个bloc,我想使用一个线程。该线程用于从UDP套接字获取数据,以便我可以在GNU无线电组中使用它。“常规工作”功能是完成所有信号和数据处理的功能 主源文件的组织方式如下: namespace gr { namespace adsb { out::sptr out::make() { return gnuradio::get_initial_sptr (new out_impl()); }

我目前正在GNU电台上开发一个bloc,我想使用一个线程。该线程用于从UDP套接字获取数据,以便我可以在GNU无线电组中使用它。“常规工作”功能是完成所有信号和数据处理的功能

主源文件的组织方式如下:

namespace gr {
    namespace adsb {

    out::sptr
    out::make()
    {
        return gnuradio::get_initial_sptr
        (new out_impl());
    }

    /*
     * UDP thread
     */
    void *task_UdpRx (void *arg)
    {
        while(true)
        {
            printf("Task UdpRx\n\r");
            usleep(500*1000);
        }
       pthread_exit(NULL);
    }

    /*
    * The private constructor
    */
    out_impl::out_impl()
        : gr::block("out",
                gr::io_signature::make(1, 1, sizeof(int)),
                gr::io_signature::make(1, 1, sizeof(char)))
    {
        pthread_t Thread_UdpRx;

        //Thread init
        if(pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))
        {
            err("Pthread error");
        }
        else
        {
            printf("UDP thread initialization completed\n\r");
        }
    }

    /*
    * Our virtual destructor.
    */
    out_impl::~out_impl()
    {
    }

    void out_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
    {
        ninput_items_required[0] = noutput_items;
    }

    int out_impl::general_work (int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
    {
        const int *in = (const int *) input_items[0];
        char *out = (char *) output_items[0];

        // Do <+signal processing+>
        for(int i = 0; i < noutput_items; i++)
        {
            printf("General work\n\r");
        }/* for < noutput_items */

        // Tell runtime system how many input items we consumed on
        // each input stream.
        consume_each (noutput_items);

        // Tell runtime system how many output items we produced.
        return noutput_items;
    } /* general work */

    } /* namespace adsb */
} /* namespace gr */`
此错误指的是行,它与任务_UdpRx有关:

if(pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))
有人知道吗

如果需要,请随时询问更多细节。我显示的代码是我所能做的最短的代码,以便您尽可能地获得最佳理解


谢谢大家!

不能将指向成员函数的指针传递给
pthread\u create()

您将需要一个免费函数:

void *thread_start(void *object)
{
    gr::adsb::out_impl *object = reinterpret_cast<gr::adsb::out_impl *>(object);
    object.task_UpdRx(0);
    return 0;
}
我保留滥用
重新解释cast()
-替换正确的cast符号的权利。我对在构造函数中启动线程持保留态度,但这是一个应该有效的方案的概要


2014年6月25日代码更新后 代码仍然不是MCVE。我不能复制和编译它。缺少许多标题。未定义/声明类型
out
out\u impl
。一些GNU无线电继承的东西可能会被忽略掉

这是我的MCVE版本。它不是完美的,因为它不会重现你的问题。它在Ubuntu12.04 LTS的派生版本和GCC4.9.0上干净地编译。它假定您有一个定义函数
err()
的头
。我不确定析构函数是否是MCVE所必需的(移除它将消除另外5行左右)

来源 下一步 从这里开始,问答有几种方式:

  • 这个问题被放弃或结束了
  • 您获取MCVE代码并添加内容,直到得到原始编译错误
  • 您可以获取非编译代码并删除内容,直到您到达一个MCVE,在该MCVE中,您可以不删除错误而不删除任何内容。代码最好不要引用在常规Linux安装中找不到的任何头。如果必须的话,你需要确定它们是从哪里获得的——是的,如果必须的话,我可能会找到GNU收音机,但我不需要这样做

  • 它需要一个自由指针,而您正试图将其传递给一个成员函数,因此我认为“可能的重复”不是重复的,这并不奇怪;它询问如何将指针传递到预期的成员函数,而这需要设计一种机制来传递指针到函数(不是成员函数)并仍然到达类。但是定义
    void*task\u UdpRx(void*arg)
    看起来像自由函数,不是吗?@glutton:从错误消息判断,
    task\u UdpRx
    作为类范围内的成员函数编写。当然,如果我错了,那么我们需要一个SSCCE()或MCVE(),这样我们就可以修改代码。我使用了您建议的免费函数,但我仍然有一个编译错误:请求“object”中的成员“task\u UpdRx”,这是非类类型的“gr::adsb::out\u impl*”。此错误指行对象。taskUdpRx(0);自由函数的定义。OK;是时候创建一个MCVE()或SSCCE(),两个名称代表同一事物了。(当你修改了问题后,请在这里写一条评论,这样我就知道要回来看;否则我可能记不起来了。)@JonathanLeffler我已经用我能提供的最简单代码修改了原始问题。谢谢你以后的帮助!
    void *thread_start(void *object)
    {
        gr::adsb::out_impl *object = reinterpret_cast<gr::adsb::out_impl *>(object);
        object.task_UpdRx(0);
        return 0;
    }
    
    if (pthread_create(&Thread_UdpRx, NULL, thread_start, this))
    
    #include <cstdio>
    #include <pthread.h>
    #include <unistd.h>
    #include <err.h>
    
    namespace gr
    {
        namespace adsb
        {
            class out_impl
            {
            public:
                out_impl();
                virtual ~out_impl();
            };
    
            void *task_UdpRx(void *arg)
            {
                while (true)
                {
                    printf("Task UdpRx %p\n\r", arg);
                    usleep(500*1000);
                }
                pthread_exit(NULL);
            }
    
            out_impl::out_impl()
            {
                pthread_t Thread_UdpRx;
    
                if (pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))
                    err(1, "Pthread error");
                else
                    printf("UDP thread initialization completed\n\r");
            }
    
            out_impl::~out_impl()
            {
            }
    
        }
    }
    
    $ g++ --version
    g++ (GCC) 4.9.0
    Copyright (C) 2014 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ make gr.o
    g++ -g -O3 -std=c++11 -Wall -Wextra -Werror -c gr.cpp
    $ nm -C -g gr.o
    0000000000000010 T gr::adsb::task_UdpRx(void*)
    0000000000000050 T gr::adsb::out_impl::out_impl()
    0000000000000050 T gr::adsb::out_impl::out_impl()
    0000000000000040 T gr::adsb::out_impl::~out_impl()
    0000000000000000 T gr::adsb::out_impl::~out_impl()
    0000000000000000 T gr::adsb::out_impl::~out_impl()
    0000000000000000 V typeinfo for gr::adsb::out_impl
    0000000000000000 V typeinfo name for gr::adsb::out_impl
                     U vtable for __cxxabiv1::__class_type_info
    0000000000000000 V vtable for gr::adsb::out_impl
                     U operator delete(void*)
                     U err
                     U printf
                     U pthread_create
                     U usleep
    $