C++ 模板函数错误问题

C++语言 码拜 8年前 (2016-09-23) 1987次浏览
.h
class ConfigReader
{
private:
struct KVPair
{
std::string key;
std::string key_value;
KVPair(){;}; //结构体的构造函数
KVPair(const KVPair& kv) :key(kv.key),key_value(kv.key_value){;} //结构体的构造函数2;拷贝构造函数;
//操作符重载
const KVPair& operator= (const KVPair& kv)
{
key = kv.key;
key_value = kv.key_value;
return *this;
}
bool const operator ==(const KVPair& kv){ return (key==kv.key); }
bool operator <(const KVPair& kv){return (key_value<kv.key_value);}

};
deque<KVPair> key_value_table;
.cpp
有个模板函数
template<class C, class T>
bool find_sorted(unsigned int&rv, const C& container,const T& element)
{
unsigned int right=container.size();
unsigned int left=0;
unsigned int searchpos;
if(right==0)
{
rv=0;
return false;
}
while(true)
{
if(left+1>=right)
{
if((container[left])==element)
{
rv=left;
return true;
}
else
{
if(container[left]<element)
rv=left+1;
else
rv=left;
}
}
else
{
searchpos=(left+right)/2;
if(element<(container[searchpos]))
{
right=searchpos;
}
else
{
left= searchpos;
}
}
}
}
调用这个模板函数
if(p<n)
{
KVPair new_pair;
string pure_key = remove_trailing_whitespace(line.substr(0,p));
new_pair.key = pure_key;
string tmpstr = remove_trailing_whitespace(line.substr(p+1,n-p-1));
string tmpstr2 = replace_all(tmpstr,string(“$PATH”),directorty_stack.top());
new_pair.key_value = replace_all(tmpstr2,string(“$HOME”),home_path);
unsigned int pos;
if(verbosity==3)
cerr << “ConfigRead : read line” << new_pair.key << “\n”;
if(find_sorted(pos,key_value_table,new_pair))
编译报错:
ConfigReader.cpp
1>c:\users\luoyang.win-a98iv7hj3bi\desktop\vstest0628\operate\operate\configreader.cpp(109): error C2678: 二进制“==”: 没有找到接受“const ConfigReader::KVPair”类型的左操作数的运算符(或没有可接受的转换)
1>          d:\program files\microsoft visual studio 10.0\vc\include\exception(470): 可能是“bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)”
1>          d:\program files\microsoft visual studio 10.0\vc\include\exception(475): 或       “bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)”
1>          d:\program files\microsoft visual studio 10.0\vc\include\exception(481): 或       “bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)”
1>          d:\program files\microsoft visual studio 10.0\vc\include\system_error(408): 或       “bool std::operator ==(const std::error_code &,const std::error_condition &)”
1>          d:\program files\microsoft visual studio 10.0\vc\include\system_error(416): 或       “bool std::operator ==(const std::error_condition &,const std::error_code &)”
1>          c:\users\luoyang.win-a98iv7hj3bi\desktop\vstest0628\operate\operate\configreader.h(32): 或       “bool ConfigReader::KVPair::operator ==(const ConfigReader::KVPair &)”
1>          尝试匹配参数列表“(const ConfigReader::KVPair, const ConfigReader::KVPair)”时
1>          c:\users\luoyang.win-a98iv7hj3bi\desktop\vstest0628\operate\operate\configreader.cpp(347): 参见对正在编译的函数 模板 实例化“bool `anonymous-namespace”::find_sorted<std::deque<_Ty>,ConfigReader::KVPair>(unsigned int &,const C &,const T &)”的引用
1>          with
1>          [
1>              _Ty=ConfigReader::KVPair,
1>              C=std::deque<ConfigReader::KVPair>,
1>              T=ConfigReader::KVPair
1>          ]

重载一个结构体的操作符应该没有问题;本人不知道问题出在哪里,感觉调用和定义都没问题啊;key_value_table是一个
deque<KVpair>类型的,在class里面定义的,作为一个模板函数一个参数,另一个模板函数的参数是KVpair;求指导答
解决方案

40

bool const operator ==(const KVPair& kv) const { return (key==kv.key); }
下同

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++ 模板函数错误问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)