c++模板-参数中类型/值不匹配
C++ template - type/value mismatch at argument
我得到这些错误,
模板形参列表中参数1的类型/值不匹配'模板类sasengineequeue '
模板形参列表中参数2的类型/值不匹配'模板类sasengineequeue '
,但如果我使用std::string的SasEngineQueue类的模板参数,那是可以的,但正如下面的代码,如果我使用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();
您应该始终指出错误发生的确切位置-这使人们更容易回答。
无论如何,问题是,虽然std::string
是一个类型(具体来说,std::basic_string<char>
的类型定义),std::vector
不是。这是一个模板,你需要指定vector包含的类型,例如std::vector<int>
。
您可能想让它适用于任何向量——不幸的是,这不起作用,因为显式实例化必须指定一个具体类型。
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- 将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 不同c++编译器之间的自动类型推导不匹配
- 重载函数的地址与所需类型不匹配
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 参数错误可能与类型不匹配有关?
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- 模板参数列表中参数 3 处的类型/值不匹配
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 调用子例程时类型不匹配
- 如果方法不进行类型检查,为什么C++模板匹配?
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 警告 634:相等或有条件的强类型不匹配(类型"bool")