2011-11-03 31 views
2

我想根據用戶輸入的內容創建一個整數,雙精度或字符串數​​組。程序詢問用戶有多少個對象在數組中,然後詢問對象。完成後,用戶可以搜索一個對象。動態創建數組 - 無法推斷出'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 

回答

4

模板函數嘗試從傳入參數的類型推導出模板類型。

顯然,如果函數沒有參數,函數不能做到這一點。 在這種情況下,你必須明確地調用函數時註明的模板類型:

所以,在你的主要功能:

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

你也需要做這裏面linearSearchProg

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

非常感謝,我只是希望能解決最後一個錯誤,並在我的問題中更新。 – michaellindahl

3
template<typename T> void linearSearchProg(); 

linearSearchProg需要一個模板參數,不能推斷從func (因爲它沒有)。在main()更進一步嘗試調用它不提供模板參數:

linearSearchProg(); 

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

linearSearchProg<double>(); 

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

+0

我不知道它會有什麼類型,因爲這在運行時是由用戶決定的。我想這是不可能的,我會重寫我的代碼,詢問用戶他們會給我什麼樣的輸入。 – michaellindahl

+0

@michaellindahl:模板是編譯時的東西,所以這就是你所能得到的。您無法在運行時實例化模板。 –

+0

好的,謝謝你的快速回復! – michaellindahl