65,206
社区成员
发帖
与我相关
我的任务
分享
我在自己的模块中定义了一个用于比较std::bitset大小的模板函数:
template<size_t _N>
bool operator<(const std::bitset<_N> &lhs,const std::bitset<_N> &rhs)
{
return lhs.to_string() < rhs.to_string();
}
如果程序中对两个bitset变量比较大小,没有问题,但我比较两个
std::pair<bitset<N>,bitset<N> >类型的变量时,就会提示没有可匹配的operator<,
那位大牛给指点一下原因是什么?先谢过了:)
#include <bitset>
#include <utility>
#include <iomanip>
#include <iostream>
using namespace std;
template <size_t N>
bool operator<(const bitset<N> & lhs,const bitset<N>& rhs)
{
return lhs.count()<rhs.count();
}
pair<bitset<100>,bitset<100> > a,b;
int main()
{
bitset<100> l(10),r(20);
cout<<(l<r)<<endl;
a=make_pair(r,r);
b=make_pair(l,l);
cout<<(a<b)<<endl;
}
namespace test{
class A{//means bitset<>
public:
int aa;
};
template <class T>
class B{//means pair<>
public:
T BT;
};
template <class T>
bool operator<(B<T> a,B<T> b)
{
return a.BT<b.BT;
}
}
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
int main()
{
test::B<test::A> B1,B2;
B1<B2;
return 0;
}
TTT.cpp: In function `bool test::operator<(test::B<T>, test::B<T>) [with T =
test::A]':
TTT.cpp:28: instantiated from here
TTT.cpp:15: no match for `test::A& < test::A&' operator
namespace test{
class A{//means bitset
public:
int aa;
};
template <class T>
class B{//means pair
public:
T BT;
};
template <class T>
bool operator<(B<T> a,B<T> b)
{
return a.BT<b.BT;
}
}
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
//HERE!!!
bool operator<(test::B<test::A> a,test::B<test::A> b)
{
return a.BT<b.BT;
}
int main()
{
test::B<test::A> B1,B2;
B1<B2;
return 0;
}
#include <bitset>
#include <utility>
#include <iomanip>
#include <iostream>
using namespace std;
template <size_t N>
bool operator<(const bitset<N> & lhs,const bitset<N>& rhs)
{
return lhs.count()<rhs.count();
}
pair<bitset<100>,bitset<100> > a,b;
int main()
{
bitset<100> l(10),r(20);
cout<<(l<r)<<endl;
a=make_pair(r,r);
b=make_pair(l,l);
cout<<(a<b)<<endl;
}
namespace test{
class A{//means bitset<>
public:
int aa;
};
template <class T>
class B{//means pair<>
public:
T BT;
};
template <class T>
bool operator<(B<T> a,B<T> b)
{
return a.BT<b.BT; // 其实编译报错的问题在这里
}
}
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
int main()
{
test::B<test::A> B1,B2;
B1<B2; // 这里调用了实例化的 bool operator<(B<T> a,B<T> b)
return 0;
}
return a.BT<b.BT;
namespace test{
class A{//means bitset
public:
int aa;
};
template <class T>
class B{//means pair
public:
T BT;
};
template <class T>
bool operator<(B<T> a,B<T> b)
{
return a.BT<b.BT;
}
}
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
//HERE!!!
bool operator<(test::B<test::A> a,test::B<test::A> b)
{
return a.BT<b.BT;
}
int main()
{
test::B<test::A> B1,B2;
B1<B2; // 这里显然解决了你上个例子的缺少的 operator<(A<> &, A<> &) 的问题. 因此通过编译
return 0;
}
namespace test{
class A{//means bitset
public:
int aa;
};
template <class T>
class B{//means pair
public:
T BT;
};
template <class T>
bool operator<(B<T> a,B<T> b)
{
return a.BT<b.BT;
}
}
#if 0
//在此处则不能使用
void test_name_resolver()
{
test::B<test::A> B1,B2;
B1<B2;
}
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
//HERE!!!
bool operator<(test::B<test::A> a,test::B<test::A> b)
{
return a.BT<b.BT;
}
int main()
{
//此处可以使用
test::B<test::A> B1,B2;
B1<B2;
return 0;
}
#else
//在此处可以使用
void test_name_resolver()
{
test::B<test::A> B1,B2;
B1<B2;
}
namespace test{
bool operator<(test::A a,test::A b)
{
return a.aa<b.aa;
}
//HERE!!!
bool operator<(test::B<test::A> a,test::B<test::A> b)
{
return a.BT<b.BT;
}
}
int main()
{
//此处也可以使用
test::B<test::A> B1,B2;
B1<B2;
return 0;
}
#endif