Thread: Getting a little confused with lists...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Getting a little confused with lists...

    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!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> list<T>::const_iterator iter;
    >> for (iter=aList.begin();iter!=aList.end(); iter++)
    >> if (*iter==item)

    Don't worry about C4786, that's just debug information that "doesn't fit".

    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    That's awesome! Thank you very much! I was a lot closer to the solution than I thought I was, so that's encouraging as well... thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 03:59 PM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM