Hi all,
I wrote a program to count the number of times an item appears in a list using a sequential search. I had numerous errors and corrected most of them by way of online tutorials, loking up compiler error codes, and textbooks. I still have a few errors that I can't seem to correct, basically something is wrong with the way I'm declaring the functions as templates. Here's the code with the compiler errors.
Code:
#include<iostream>
#include<string>
#include<list>
using namespace std;
template <typename T>
int count(const list<string>& aList, const T& item);
template <typename T>
typename list<T>::iterator seqSearch(typename list<T>::const_iterator first, typename list<T>::const_iterator last, const T& target);
int main() {
string arr[] = { "the", "boy", "the", "girl", "the", "person", "the man" };
int arrSize = sizeof(arr) / sizeof(*arr);
list<string> strList(arr, arr + arrSize);
list<string>::const_iterator iter = strList.begin();
int wordCount = count(strList, *iter);
cout << endl << "The number of times 'the' is in the list is: " << wordCount << endl << endl;
return 0;
}
template <typename T>
int count(const list<string>& aList, const T& item){
int itemCount = 0;
//set iterator to the beginning of the list
list<T>::const_iterator iter = aList.begin();
//make repeated calls to seqSearch until we reach the end
while (iter != aList.end()){
iter = seqSearch(iter, aList.end(), item);
if (iter != aList.end()){
//seqSearch will only return an iterator when item is found in the list
itemCount++;
iter++;
}
}
return itemCount;
}
template <typename T>
typename list<T>::iterator seqSearch(typename list<T>::const_iterator start, typename list<T>::const_iterator last, const T& target){
//compare list elements with target until either we arrive
//at last or locate target
while (start != last && (*start != target))
start++;
//iter either points to target or is last
return start;
}
Code:
------ Build started: Project: chapt6_quest23b, Configuration: Debug Win32 ------
Compiling...
seqSearch2.cpp
c:\documents and settings\welkj01\my documents\visual studio 2005\projects\chapt6_quest23b\chapt6_quest23b\seqsearch2.cpp(56) : error C2664: 'std::list<_Ty>::_Iterator<_Secure_validation>::_Iterator(std::_List_nod<_Ty,_Alloc>::_Node *,const std::list<_Ty> *)' : cannot convert parameter 1 from 'std::list<_Ty>::_Const_iterator<_Secure_validation>' to 'std::_List_nod<_Ty,_Alloc>::_Node *'
with
[
_Ty=std::string,
_Secure_validation=true,
_Alloc=std::allocator<std::string>
]
and
[
_Ty=std::string,
_Secure_validation=true
]
and
[
_Ty=std::string,
_Alloc=std::allocator<std::string>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\documents and settings\welkj01\my documents\visual studio 2005\projects\chapt6_quest23b\chapt6_quest23b\seqsearch2.cpp(35) : see reference to function template instantiation 'std::list<_Ty>::_Iterator<_Secure_validation> seqSearch<T>(std::list<_Ty>::_Const_iterator<true>,std::list<_Ty>::_Const_iterator<true>,const T &)' being compiled
with
[
_Ty=std::string,
_Secure_validation=true,
T=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
]
c:\documents and settings\welkj01\my documents\visual studio 2005\projects\chapt6_quest23b\chapt6_quest23b\seqsearch2.cpp(19) : see reference to function template instantiation 'int count<std::basic_string<_Elem,_Traits,_Ax>>(const std::list<_Ty> &,const T &)' being compiled
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>,
_Ty=std::string,
T=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
]
Build log was saved at "file://c:\Documents and Settings\welkj01\My Documents\Visual Studio 2005\Projects\chapt6_quest23b\chapt6_quest23b\Debug\BuildLog.htm"
chapt6_quest23b - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thank you in advance for your time and any help you can provide.