Hello all :)
Working on an algorithm to look at a STL container of STL strings (or other strings, making it general)
Basically it loops through something like a std::list and returns the length of the longest beginning in common. It's for processing lists of files, like this:
C:\Windows\System32\Stuff.exe C:\Windows\Things\InHere.txt C:\Windows\Foo\Bar.txt
This should return 11, because "C:\Windows\
" is in common.
Never written a templatized function before, and my compiler is complaining. Here's my code:
Header:
// longestBegin.h -- Longest beginning subsequence solver
template <typename SequenceSequenceT, typename SequenceT, typename T >
size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates);
Implementation:
// longestBegin.cpp -- Longest beginning subsequence solver
#include <stdafx.h>
template <typename SequenceSequenceT, typename SequenceT, typename T >
size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
{
SequenceT firstString = *firstCandidates;
size_t longestValue = firstString.length();
firstCandidates++;
for(size_t idx = 0; idx < longestValue; idx++)
{
T curChar = firstString[idx];
for(InputIterator curCandidate = firstCandidates;curCandidate != lastCandidates; curCandidate++)
{
if ((*curCandidate)[idx] != curChar)
return idx - 1;
}
}
return longestValue;
}
I have a funny feeling I'm missing something fundamental here......
The compiler bombs with the following error:
error C2998: 'size_t longestBegin' : cannot be a template definition
Any ideas? Thanks!
Billy3