簡體   English   中英

動態創建數組-無法推斷出'T'的模板參數-C ++

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

我試圖根據用戶輸入內容創建一個整數,雙精度或字符串數​​組。 該程序詢問用戶數組中有多少個對象,然后詢問這些對象。 完成此操作后,用戶可以搜索對象。

請幫助解決此錯誤:

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內部執行此linearSearchProg

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

linearSearchProg采用模板參數,該參數不能從函數參數中推導(因為它沒有)。 main()您進一步嘗試在不提供template參數的情況下調用它:

linearSearchProg();

實際上應該是這樣的,例如:

linearSearchProg< double >();

對於您要實現的特定邏輯,我認為linearSearchProg不需要模板參數。 您將需要知道您將要支持的所有類型,並以某種方式打開用戶輸入以使用適當的模板參數來調用模板函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM