|
-
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)
==
-
March 3rd, 2006, 02:50 AM
#2
Re: Is it a proper way to use count with user defined classes?
My comments are inline with the code....
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CMyClass
{
int m_nInt;
public :
CMyClass() : m_nInt (0) {} //use constructor intialization list
/*explicit*/ CMyClass(const int nInt) : m_nInt(nInt) {} //use constructor intialization list - make it explicit
//you do not need the copy constructor for this class - the compiler will provide one for you.
//you do not need the copy assignment operator for it - the compiler will provide one for you.
//here you had a typo in function name - it should be operator== and not operator=
bool operator == (const CMyClass& rMyClass) const //this function should be const
{
return (m_nInt == rMyClass.m_nInt);
}
bool operator!=(const CMyClass& rhs) const{
return (m_nInt != rhs.m_nInt);
}
bool operator > (const CMyClass& rMyClass) const //this function shoould be const
{
return (m_nInt > rMyClass.m_nInt);
}
bool operator < (const CMyClass& rMyClass) const //this function shoould be const
{
return (m_nInt < rMyClass.m_nInt);
}
bool operator > (const int nInt) const //this function shoould be const
{
return (m_nInt > nInt);
}
bool operator < (const int nInt) const ////this function shoould be const
{
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;
}
This compiles fine on Comeau online.. I added another operator!=. Seems like std::count needs it as well.. that is wierd since it should simply use operator== and not necessarily need operator!=. Here:
Code:
bool operator!=(const CMyClass& rhs) const{
return (m_nInt != rhs.m_nInt);
}
Alternatively - you may want to make the int-taking constructor explicit and avoid simply adding integer literals in the vector. It will keep out confusion as to if the vector takes objects of your defined type of just integers. Also, avoid making objects before putting into the vector like you have done.. you can push_back using temporaries.. and they will last only the end of that statement and no longer - you don't need them even after addition into the vector.. something like this:
Code:
v.push_back(CMyClass(100));
You do not need the copy constructor and the copy assignment operator for such a class to be explicitly written.. keep you code concise - the compiler will provide them for your iff you need them else it wouldn't. Hope this helps. Regards.
Last edited by exterminator; March 3rd, 2006 at 06:35 AM.
Reason: missed the word explicit at one place...
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 3rd, 2006, 03:47 AM
#3
Re: Is it a proper way to use count with user defined classes?
That was fantastic.... first I changed = to == and it worked fine...
I will take care of other programming habits...(like using consts etc).
Appreciated.
Sunnypriya
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
|