0

I am making a bubble sort function but I am having trouble with call the function from the .cpp and .h file to the main.cpp file. Every time I try to call it an error comes up, the error is "undefined reference to 'void buubleSort(int*, int)'". I can't figure out how to fix it and make the code work. The main.cpp file is

int main() {
  int list[] = { 1, 10, 90, 100, -1, 11, 9, 14, 3, 2, 20, 19 };
  int size = sizeof(list)/sizeof(int);
  cout << "before sorting: " << endl;
  printArray(list, size);
  cout << "after sorting: " << endl;
  bubbleSort(list, size);
  printArray(list, size);
}

The header.h is

template <typename T>
void bubbleSort(T list[], int size);
template <typename T>
void printArray(T list[], int size);

the code.cpp is

template <typename T>
void bubbleSort(T list[], int size)
{
  bool swapped = true;
  while (swapped == true)
  {
    do {
      swapped = false;
      for (int i = 1; i < size - 1; i++)
        {
          if(list[i+1] < list[i])
          {
            swap(list[i+1], list[i]);
            swapped = true;
          }
        }
    } while (swapped == false);
  }  
}

template <typename T>
void printArray(T list[], int size)
{
  for (int i = 0; i << size; i++)
    cout << list[i] << " ";
  cout << "\n\n";
}

0 Answers0