I'm having an error in my code that says the float and string data types aren't proper function calls for my template function. The template function works for int and char, but not float and string. I looked to see if there was errors in the function definitions, but I couldn't see any. Can someone explain or suggest what might be the problem?
Search.h
#ifndef Search_hpp
#define Search_hpp
#include <iostream>
#include <stdio.h>
using namespace std;
class Search{
private:
public:
template<typename ST>
ST LinearSearch(ST numbers[], int listSize, ST key);
template<typename ST>
ST BinarySearch(ST numbers[], int listSize, ST key);
};
#endif /* Search_hpp */
Search.cpp
#include "Search.h"
#include <iostream>
#include <string>
using namespace std;
template<typename ST>
ST Search::LinearSearch(ST numbers[], int listSize, ST key){
int i;
for (i = 0; i < listSize; ++i) {
if (numbers[i] == key) {
return i;
}
}
return -1; /* not found */
}
template<typename ST>
ST Search::BinarySearch(ST numbers[], int listSize, ST key){
int mid;
int low;
int high;
low = 0;
high = listSize - 1;
while (high >= low) {
mid = (high + low) / 2;
if (numbers[mid] < key) {
low = mid + 1;
}
else if (numbers[mid] > key) {
high = mid - 1;
}
else {
return mid;
}
}
return -1; // not found
}
Main.cpp
#include "Search.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
int integerlist[] = {-1, 1, 5, 7, 8};
float floatlist[] = {0.3338, 0.5, 2.5, 7.2, 9.6};
string stringlist[] = {"anteater", "cat", "giraffe", "lion", "octopus"};
char charlist[] = {'a', 'b', 'i', 'l', 'o' };
const int size = 5;
Search SearchClass;
SearchClass.LinearSearch(integerlist, size, 12);
SearchClass.BinarySearch(integerlist, size, 12);
SearchClass.LinearSearch(floatlist, size, 6.543);
SearchClass.BinarySearch(floatlist, size, 6.543);
SearchClass.LinearSearch(stringlist, size, "lion");
SearchClass.BinarySearch(stringlist, size, "lion");
SearchClass.LinearSearch(charlist, size, 'o');
SearchClass.BinarySearch(charlist, size, 'o');
};