|
-
March 3rd, 2006, 02:14 AM
#1
Is it a proper way to use count with user defined classes?
Hi, I am attaching error and the code which caused this error. Can any body help?
Code
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CMyClass
{
int m_nInt;
public :
CMyClass()
{
m_nInt = 0;
}
CMyClass(const int nInt)
{
m_nInt = nInt;
}
CMyClass(const CMyClass& rMyClass)
{
m_nInt = rMyClass.m_nInt;
}
bool operator = (const CMyClass& rMyClass)
{
return (m_nInt == rMyClass.m_nInt);
}
bool operator > (const CMyClass& rMyClass)
{
return (m_nInt > rMyClass.m_nInt);
}
bool operator < (const CMyClass& rMyClass)
{
return (m_nInt < rMyClass.m_nInt);
}
bool operator = (const int nInt)
{
return (m_nInt == nInt);
}
bool operator > (const int nInt)
{
return (m_nInt > nInt);
}
bool operator < (const int nInt)
{
return (m_nInt < nInt);
}
};
typedef vector<CMyClass> CMyClassVec;
int main()
{
CMyClassVec v;
CMyClass c1(20),c2(5),c3(50),c4(2),c5(50);
v.push_back(10);
v.push_back(c1);
v.push_back(100);
v.push_back(c2);
v.push_back(c3);
v.push_back(c4);
v.push_back(c5);
int quantity = 0;
quantity = count(v.begin(),v.end(),c2);
cout << "object equal to c2 = " << quantity << endl;
cout << endl;
return 0;
}
Error
===
Compiling...
file.cpp
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::istream_iterator<_U,_E,_Tr> &,const class std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template argument fo
r 'const class std::istream_iterator<_U,_E,_Tr> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_
D> &)' : could not deduce template argument for 'const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_D> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector
<_Ty,_A> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocat
or<_Ty> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::istreambuf_iterator<_E,_Tr> &,const class std::istreambuf_iterator<_E,_Tr> &)' : could not deduce template argument fo
r 'const class std::istreambuf_iterator<_E,_Tr> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce te
mplate argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2784: 'bool __cdecl std: perator ==(const struct std: air<_T1,_T2> &,const struct std: air<_T1,_T2> &)' : could not deduce template argument for 'const struct std: air<
_T1,_T2> &' from 'class CMyClass'
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(72) : error C2676: binary '==' : 'class CMyClass' does not define this operator or a conversion to a type acceptable to the predefined operator
D:\Prasanna\Technical\From CplusAbout\Predicates\file.cpp(73) : see reference to function template instantiation 'int __cdecl std::count(class CMyClass *,class CMyClass *,const class CMyClass &)' being compiled
Error executing cl.exe.
Predicates.exe - 8 error(s), 0 warning(s)
==
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|