Hello all, I have a programming assignment that is driving me up the wall. I need to change a program that uses parallel dynamic arrays to one that uses a single array of structs. But whenever I try to compile, Visual Studio returns the errors
"C2782: 'int addInOrder(T *,int &,T)' : template parameter 'T' is ambiguous"
"C2782: 'int binarySearch(const T [],int,T)' : template parameter 'T' is ambiguous"
The same code in CodeBlocks returns these two errors:
"error: no matching function for call to `binarySearch(CountedWords*&, int&, std::string&)'"
"error: no matching function for call to `addInOrder(CountedWords*&, int&, std::string&)'"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
struct CountedWords
{
string word;
int count;
};
template <typename T>
int addInOrder (T* array, int& size, T value)
{
// Make room for the insertion
int toBeMoved = size - 1;
while (toBeMoved >= 0 && value < array[toBeMoved].word) {
array[toBeMoved+1].word = array[toBeMoved].word;
--toBeMoved;
}
// Insert the new value
array[toBeMoved+1].word = value;
++size;
return toBeMoved+1;
}
// Search an ordered array for a given value, returning the index where
// found or -1 if not found.
template <typename T>
int binarySearch(const T list[], int listLength, T searchItem)
{
int first = 0;
int last = listLength - 1;
int mid;
bool found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid].word == searchItem)
found = true;
else
if (searchItem < list[mid].word)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
|
Inside a void, calls to the other functions looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void histogram(const int MaxWords, istream& input, ostream& output)
{
int nWords = 0;
string word;
CountedWords* wordsWithCounts = new CountedWords[MaxWords];
while (input >> word)
{
int index = binarySearch(wordsWithCounts, nWords, word);
addInOrder(wordsWithCounts, nWords, word);
}
}
|
When the same call is made with a regular dynamic array of strings or ints, the program compiles just fine. Can someone help me understand what is wrong and why?