【发布时间】:2011-02-16 12:05:53
【问题描述】:
我在使用模板成员重载运算符和使用 make_pair 时遇到一些问题:
class MyArchive
{
public:
template <class C> MyArchive & operator<< (C & c)
{
return (*this);
}
};
class A
{
};
int main()
{
MyArchive oa;
A a;
oa << a; //it works
oa << std::make_pair(std::string("lalala"),a); //it doesn't work
return 0;
}
我得到了这个有趣的错误:
/home/carles/tmp/provaserialization/main.cpp: In function ‘int main()’:
/home/carles/tmp/provaserialization/main.cpp:30: error: no match for ‘operator<<’ in ‘oa << std::make_pair(_T1, _T2) [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = A]((a, A()))’
/home/carles/tmp/provaserialization/main.cpp:11: note: candidates are: MyArchive& MyArchive::operator<<(C&) [with C = std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, A>]
关于为什么在第二种情况下找不到 operator<< 有什么想法吗?
【问题讨论】:
标签: c++ templates operator-overloading