使用模板的C++静态编译错误

标签 c++ templates

我有

template < typename threadFuncParamT >
class ThreadWrapper
{
public:
    static int ThreadRoutineFunction(void* pParam);
    int ExecuteThread();

    ThreadWrapper(ThreadPool<threadFuncParamT> *pPool);

};

template<typename threadFuncParamT>
int ThreadWrapper<threadFuncParamT>::ThreadRoutineFunction(void* pParam)
{
    ThreadWrapper<threadFuncParamT> *pWrapper = reinterpret_cast<ThreadWrapper<threadFuncParamT>*>(pParam);
        if(pWrapper != NULL)
{

        return pWrapper-ExecuteThread(); // Error here.
    }

    return 0;
}

template < typename threadFuncParamT >
ThreadWrapper<threadFuncParamT>::ThreadWrapper(ThreadPool<threadFuncParamT> *pPool)
{
    ThreadWrapper<threadFuncParamT>::m_pThreadPool = pPool;
    m_tbbThread = new tbb::tbb_thread(&(ThreadWrapper<threadFuncParamT>::ThreadRoutineFunction), this);
    if (m_tbbThread->native_handle() == 0)
    {
        delete m_tbbThread;
        m_tbbThread = NULL;
        // TODO: throw execption here or raise assert.
    }
}

我收到如下错误 错误1 error C2352: 'ThreadWrapper::ExecuteThread' : 非法调用非静态成员函数

我在 VS2010 上编译。

谁能帮我解决这个错误。

谢谢!

最佳答案

问题是你的错误行

return pWrapper-ExecuteThread(); // Error here.

遗漏了一个>;应该是

return pWrapper->ExecuteThread(); // Error here.

你得到了这样一个奇怪的编译错误,因为它试图执行减法;指针 pWrapper 被视为整数,并从中减去通过调用 ExecuteThread()(产生 int)返回的值。但是,ExecuteThread() 既不是全局函数也不是静态成员函数 - 因此编译器会报错。

关于使用模板的C++静态编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941478/

相关文章:

java - Java 扩展是否可以在 Play Framework 上运行?

c++ - 模棱两可的重载模板

python - Django 检查表单输入字段是否不为空

C++ - 从文件读取到双

c++ - 如何递归复制文件和目录

c++ - 在 SFML 中获取文本的尺寸

c++ - 在 C++ 中对带参数(字符串)的函数使用 std bind

c++ - 来自不同命名空间的模板模板参数可以成为 friend 吗?

c++ - QLocalSocket/QDataStream应该如何读取才能避免死锁?

c++ - 如何标记字符串