2015-09-26 57 views
-1

夥計們,C++的Xcode模板函數和數組<int, i>作爲功能variabel

試圖找出原因的Xcode 6.0不喜歡這種語法

template <int i> 
int test(array<int, i> v) 
{ 
    int result = 0; 
    for (int value : v) { 
    result += value; 
    }  
return result; 
} 

int main(int argc, const char * argv[]) 
{ 
    array<int, 5> n = { { 1, 2, 3, 4, 5 } }; 
    cout << test(n); 
    return 0; 
} 

我看到的Xcode抱怨測試有關不匹配功能( n)電話。

回答

1

它不起作用,因爲std::array的第二個模板參數的類型是std::size_t而不是int。更改聲明爲:

template <size_t i> 
int test(array<int, i> v) 
+0

你是對的。 TNX隊友。 –

+0

考慮接受這個答案,如果它有幫助。 – Brian