Tarracon
Tarracon

Reputation: 55

C++ Float and String data types unable to be subbed into template function

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');
};

Upvotes: 0

Views: 681

Answers (2)

user4581301
user4581301

Reputation: 33952

LinearSearch cannot be found because of ambiguity in the type deduction for the template parameter ST

In

SearchClass.LinearSearch(floatlist, size, 6.543);

type float is inferred for ST from floatlist but double is inferred from 6.543

Solution:

SearchClass.LinearSearch(floatlist, size, 6.543F);
                                               ^
                                        change made here

for force float for both.

SearchClass.LinearSearch(stringlist, size, "lion");

is similar. string is inferred from stringlist, but "lion" is a const char[5]

SearchClass.LinearSearch(stringlist, size, string("lion"));

solves that by transforming "lion" into a string.

Unfortunately then LinearSearch tries to return the index of the found item (or -1 if not found) as a string. As suggested by forthe's answer, returning string for an index is kind of silly, as is returning anything but an integer type.

int Search::LinearSearch(ST numbers[], int listSize, ST key)

seems more appropriate.

Additional notes:

Trass3r raises a very important point in the comments. Templates only be implemented in the header file.

The magic number 5 in

const int size = 5;

can be replaced with

SearchClass.LinearSearch(integerlist, size(integerlist), 12);

as of C++17. Documentation on std::size.

Upvotes: 3

dthusian
dthusian

Reputation: 378

The problem is that your template functions are declared to return the type specified, but if it's not found, it returns -1. -1 is not a string value, so the code fails. A solution would be to change the return type to size_t or similar, as you will only be returning indices.

Upvotes: 1

Related Questions