Here's what I am attempting to do.. I attempting to make a function called count that will take in a const <list> and a n item as arguments. The function will scan through the list and count up how many times the item appears in the list. I made the function and everything templated so that it could be used for multiple data types.
Below is my code and then I'll list some of the compiler errors (VC++ 6.0):
Code:
#include <iostream>
#include <list>
#include <string>
#include "d_util.h" // included for the writeList function
template <typename T>
int count(const list<T>& aList, const T& item);
int main()
{
string strArr[5]={"Mariners", "Athletics", "Angels", "Rangers", "Mariners"};
list<string> strList(strArr, strArr+5);
string matchString;
matchString="Mariners";
std::cout << "Here's the list of characters:" << endl;
writeList(strList);
std::cout << "The matching string is " << matchString << endl;
std::cout << "You've matched on "<< matchString << " " << count(strList,matchString) << " times" << endl;
return 0;
}
template<typename T>
int count(const list<T>& aList, const T& item)
{
int matchCount=0;
list<T>::iterator iter;
for (iter=aList.begin();iter=aList.end(); iter++)
{
if (iter==item)
{
matchCount++;
}
}
return matchCount;
}
Some of the errors include:
:\Program Files\Microsoft Visual Studio\VC98\ex_6_29\ex_6_29.cpp(28) : warning C4786: 'std::reverse_bidirectional_iterator<std::list<std ::basic_string<char,std::char_traits<char>,std::al locator<char> >,std::allocator<std::basic_string<char,std::cha
r_traits<char>,std::allocator<char> > > >::const_iterator,std::basic_string<char,std::char _traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const &,std::basic_string<char,std::char_traits<char>
,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
C:\Program Files\Microsoft Visual Studio\VC98\ex_6_29\ex_6_29.cpp(24) : see reference to function template instantiation 'int __cdecl count(const class std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator
<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled
C:\Program Files\Microsoft Visual Studio\VC98\ex_6_29\ex_6_29.cpp(24) : see reference to function template instantiation 'int __cdecl count(const class std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator
<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled
C:\Program Files\Microsoft Visual Studio\VC98\ex_6_29\ex_6_29.cpp(39) : error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<ch
ar> >' (or there is no acceptable conversion)
There are a couple of more errors, but I refrained from posting them in the interest of space. The one that really perplexes me is C4786. Per MSDN, that means that I have an identifier or class that is longer than 255 characters and it had to be truncated. I see no such issue with my code. I basically just want the count function to step through the list that is passed to it, compare each value of the list to the matching value that is passed as the 2nd argument of the function and if it matches increment the count and ultimately return the count when finished.
The other odd thing is that when I try utilizing the aList within the function, normally if I enter "aList." (without the quotes of course) it'll bring up a list of possible methods, etc. When I do that currently it is not doing that, which makes me think that I need to redefine the list within the function or something?
Any help that anyone has would be awesome.
Thanks as always!