简体   繁体   English

动态创建数组-无法推断出'T'的模板参数-C ++

[英]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 . 您还需要在linearSearchProg内部执行此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). linearSearchProg采用模板参数,该参数不能从函数参数中推导(因为它没有)。 Yet further in main() you attempt to call it without providing the template argument: main()您进一步尝试在不提供template参数的情况下调用它:

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. 对于您要实现的特定逻辑,我认为linearSearchProg不需要模板参数。 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. 您将需要知道您将要支持的所有类型,并以某种方式打开用户输入以使用适当的模板参数来调用模板函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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