简体   繁体   中英

Dynamically create array - could not deduce template argument for 'T' - C++

I am trying to create an array of ints, doubles, or strings depending on what the user inputs. The program asks the user how many objects are in the array and then asks for the objects. After this is done the user can search for an object.

Please help fix this error:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Build started 11/3/2011 4:06:12 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Test.unsuccessfulbuild".
1>ClCompile:
1>  main.cpp
1>m:\cs172\other\test\main.cpp(10): error C2783: 'void linearSearchProg(void)' : could not deduce template argument for 'T'
1>          m:\cs172\other\test\main.cpp(7) : see declaration of 'linearSearchProg'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.97
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks!

This is my code:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

template<typename T> void linearSearchProg();

int main() {
    linearSearchProg();
    return 0;
}


template<typename T> T* createArray();
template<typename T> T linearSearch(const T list[], T key, int arraySize);

template<typename T> void linearSearchProg() {
    cout << "Generic Linear Search" << endl;
    //int *list = createArray();
    T *list = createArray();

    // Get Value to Search For
    cout << "What would you like to search for? ";
    cin.ignore();
    int key;
    cin >> key;

    // Search for Value
    int index = linearSearch(list, key, (sizeof list)/(sizeof list[0]));
    cout << "Object " << key << " was found at index " << index << "(Number " << index+1 << ")" << endl;
}

// int* createArray() {
template<typename T> T* createArray() {
    int size;
    cout << "How many objects to you have to input? ";
    cin >> size;
    cout << "Please input objects of the same type." << endl;
    // int *list = new int[size];
    T *list = new T[size];
    for (int i = 0; i < size; i++) {
        cin.ignore();
        cout << "? ";
        getline(cin, list[i]);
    }
    cout << "Your array is as follows: ";
    for (int i = 0; i < size; i++) {
        cout << list[i] << "  ";
    }
    cout << endl;
    return list;
}

// int linearSearch(const int list[], int key, int arraySize) {
template<typename T> T linearSearch(const T list[], T key, int arraySize) {
    for (int i = 0; i < arraySize; i++) {
        if (key == list[i])
            return i;
    }
    return -1;
}

Modifications:

linearSearchProg<int>();
T *list = createArray<T>();
T key;
int index = linearSearch<T>(list, key, (sizeof list)/(sizeof list[0]));

Getting this error now:

1>m:\cs172\other\test\main.cpp(52): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>          m:\cs172\other\test\main.cpp(25) : see reference to function template instantiation 'void linearSearchProg<double>(void)' being compiled
1>m:\cs172\other\test\main.cpp(52): error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          m:\cs172\other\test\main.cpp(28) : see reference to function template instantiation 'void linearSearchProg<std::string>(void)' being compiled

Template functions attempt to deduce the template type from the types of the passed-in parameters.

Obviously, a function can not do this if the function has no parameters. In these cases, you must explicitly state the template types when invoking the functions:

So, in your main function:

int main() {
    linearSearchProg<double>();

You will also need to do this inside linearSearchProg .

template<typename T> void linearSearchProg() {
    cout << "Generic Linear Search" << endl;
    //int *list = createArray();
    T *list = createArray<T>();
template<typename T> void linearSearchProg();

linearSearchProg takes a template argument, which cannot be deduced from the function arguments (since it has none). Yet further in main() you attempt to call it without providing the template argument:

linearSearchProg();

It should actually be something like, for instance:

linearSearchProg< double >();

For the particular logic you are trying to implement, I think linearSearchProg doesn't need a template argument. You will need to know all of the types that you'd be willing to support, and somehow switch on user input to invoke the template function with the appropiate template arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM