0

I keep getting this error in my C++ code

sort.cxx: In function ‘void sort(Item*, SizeType) [with Item = int, SizeType = int]’:
sort.cxx:89:   instantiated from here
sort.cxx:17: error: no matching function for call to ‘change_the(int*&, int&, int&)’

It's strange to me why it's not working because I have another function call in the function that as been working perfectly

Here is the function with the function call

template <class Item, class SizeType>
void sort(Item nums[], SizeType input)
{
  for(int r = 0; r < input - 2; r++)
    {
      int w = findmin(data, r, input);
      change_the(nums, r, w);
    }
}

And here is the called function giving me the errors

 template <class Item, class SizeType>
    void change_the(Item nums[], SizeType r, SizeType w)
    {
      int save = nums[r];
nums[r] = nums[j];
nums[j] = save;
    }

If I call change_the(nums, r, q) outside of sort, it works. I can't figure out what is throwing it off, especially because the findmin function works fine in the sort function and has nearly identical parameters.

I am calling the sort function with an array of 10 integers.

sort(array_name, 9)

Here is what my program looks like altogether (I didn't include findmin function)

#include <iostream>
using namespace std;
    template <class Item, class SizeType>
        void sort(Item nums[], SizeType input)
        {
          for(int r = 0; r < input - 2; r++)
            {
              int w = findmin(data, r, input);
              change_the(nums, r, w);
            }
        }


template <class Item, class SizeType>
    void change_the(Item nums[], SizeType r, SizeType w)
    {
      int save = nums[r];
      nums[r] = nums[j];
      nums[j] = save;
    }


int main()
        {
        int array_name[9]
    ... I assign values to array_name at 0-9
    sort(array_name, 10)

I assign values to the array

and then I call sort as shown above

Thanks for the help!

Final Edit: my problem was that the function was called in another function before the actual function itself was defined. Thanks again

  • 1
    `switch` is a reserved keyword, call the template something else. – user657267 Nov 25 '15 at 06:49
  • Changed. I didn't put switch in my code, I changed the name to make it easier to read on here and didn't think about it! That wasn't what was throwing me the error. – thewalruswaspaul Nov 25 '15 at 06:51
  • Don't forget to update the error message too, and not just edit it, copy-paste the new error message. Then try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. Then finally, the compiler doesn't output any more informational messages? If you use GCC or Clang there usually is a list of alternatives that it considered. Seeing that can help figuring out what the compiler is actually looking for. – Some programmer dude Nov 25 '15 at 06:51
  • How do you invoque `sort` function? – Tio Pepe Nov 25 '15 at 06:53
  • I'll add more code but the error is still the same error I'm getting. I changed the names of the functions before writing them here. I'm using G++ and that is the only error I'm receiving – thewalruswaspaul Nov 25 '15 at 06:54
  • In for loop in `sort` function, where did you declared `i`? – Tio Pepe Nov 25 '15 at 06:54
  • Tio yes I did. I have an array with 10 integers and called sort with the following (array_name, 9). Sorry, meant to write r instead of i – thewalruswaspaul Nov 25 '15 at 06:55
  • *Where* do you define the `change_the` function? In another source file? In a header file? If in a header file, is the header file included? If in a source file, see ["Why can templates only be implemented in the header file?"](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Some programmer dude Nov 25 '15 at 06:56
  • All of my functions are entirely defined before main is invoked in one file. There isn't a header or implementation. I've done it this way before? And my textbook does it. If that is the problem I don't know why that makes it so it doesn't work in the function call. – thewalruswaspaul Nov 25 '15 at 07:01
  • Then, do you declare (or define) the function *before* you use it? – Some programmer dude Nov 25 '15 at 07:05
  • I declare it before I use it in main but I do not declare change_the before sort. I'll try changing that, although findmin is also declared after sort and seems to work. – thewalruswaspaul Nov 25 '15 at 07:07
  • It compiles! Thank you! That seemed to be the problem. I understand why it wasn't working now. Strange that one of the functions worked and the other didn't. Thanks again! – thewalruswaspaul Nov 25 '15 at 07:08

0 Answers0