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

标签 c++ templates

我正在尝试根据用户输入的内容创建一个整数、 double 或字符串数​​组。该程序询问用户数组中有多少对象,然后询问对象。完成后,用户可以搜索对象。

请帮助修复此错误:

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 ==========

谢谢!

这是我的代码:

#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;
}

修改:

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

现在得到这个错误:

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

最佳答案

模板函数试图从传入参数的类型中推导出模板类型。

显然,如果函数没有参数,则函数不能执行此操作。 在这些情况下,您必须在调用函数时显式声明模板类型:

因此,在您的主要功能中:

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

您还需要在 linearSearchProg 中执行此操作。

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

关于c++ - 动态创建数组 - 无法推断 'T' 的模板参数 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8003354/

相关文章:

c++ - 缩小从 double 到 float 的转换范围

c++ - 具有不同返回类型的成员函数的模板化别名

javascript - 从handlebars.js 中的部分访问父范围

c++ - C++ 指针可以影响 Windows 文件吗?

c++ - 使用多线程的 C++ 中的 Mandelbrot 图像生成器 "overwrites(?)"一半图像

c++ - 如果我在 GCC 中编译和链接不需要的库会怎样?

c++ - 模板和重载 istream

c++ - 模板类中 float 和 double 文字的函数特化

c++ - 在模板构建器模式中排除重复的构造函数调用

c++ - 类型定义非类型模板参数