Passing dynamic array of structs to functions

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?
You have a mismatch on the parameters. You are passing a pointer to a struct as the first parameter but a string as the third. that will not work. T can only be converted to one type. There is only one template parameter for that function. T could be a string and T* would have to be a pointer to a string. Or T* could be a pointer to a struct and T would have to be the type of that struct. If you want to pass two completely different types then you have to define two template types for the function.
int addInOrder (T* array, int& size, T value)

Think of it this way. When you call the function you are substituting a type for the T and the compiler generates an equivalent function where all Ts are substituted with the type that you specify.
Thank You, Thank You, Thank You! I sincerely had no idea about how exactly templates worked. Given your information I changed the first argument to:
1
2
template <typename T>
int addInOrder (CountedWords *array, int& size, T value)

and
1
2
template <typename T>
int binarySearch(const CountedWords *list, int listLength, T searchItem)

and it compile with no errors.

Thanks Again!
Topic archived. No new replies allowed.