C ++模板 - 参数中的类型/值不匹配

时间:2014-06-27 18:13:16

标签: c++ templates

我收到了这些错误,

  

模板参数列表中参数1的类型/值不匹配   '模板类SasEngineQueue'

     

模板参数列表中参数2的类型/值不匹配   '模板类SasEngineQueue'

但是如果我使用SasEngineQueue类的模板参数和std :: string,那没关系,但是如果我使用std :: vector作为下面的代码,则会有错误。为什么呢?


class SasEngine{
private:
    unsigned char addr;
    SasEngineQueue<std::vector, std::vector> sasQ;
    SasTCP_IP sasTransport;
};

template<typename exception_t, typename lp_t>
class SasEngineQueue{
public:
    ThreadSafeQueue<exception_t> exceptionQ;
    ThreadSafeQueue<lp_t> lpCmdQ;
    ThreadSafeQueue<lp_t> lpRspQ;
};

template<typename msgType>
class ThreadSafeQueue{
protected:
    queue<msgType> threadSafeQ;
    mutex mu;

public:
    int get(msgType& msg);
    void push(msgType msg);
    void pop();
};

template<typename msgType>
int ThreadSafeQueue<msgType>::get(msgType& msg){
    lock_guard<mutex> autoMutex(mu);

    if(threadSafeQ.empty()){
        //empty Queue
        return -1;
    }
    msg = threadSafeQ.front();
    return 0;
}

template<typename msgType>
void ThreadSafeQueue<msgType>::push(msgType msg){
    lock_guard<mutex> autoMutex(mu);
    threadSafeQ.push(msg);
}

template<typename msgType>
void ThreadSafeQueue<msgType>::pop(){
    lock_guard<mutex> autoMutex(mu);
    threadSafeQ.pop();
}

template int ThreadSafeQueue<std::vector>::get(std::vector& msg);
template void ThreadSafeQueue<std::vector>::push(std::vector msg);
template void ThreadSafeQueue<std::vector>::pop();
template int ThreadSafeQueue<std::string>::get(std::string& msg);
template void ThreadSafeQueue<std::string>::push(std::string msg);
template void ThreadSafeQueue<std::string>::pop();

1 个答案:

答案 0 :(得分:1)

您应该始终指出错误发生的确切位置 - 这使得尝试回答的人更容易。

无论如何,问题是虽然std::string是一种类型(具体来说,std::basic_string<char>的typedef),但std::vector却不是。它是一个模板,您需要指定向量包含的类型,例如std::vector<int>

你可能想让它适用于任何向量 - 遗憾的是,但这不起作用,因为显式实例化必须指定具体的类型。