2012-10-23 29 views
0

我想在我的代碼使用模板與矢量大致如下方式:矢量模板 - 「敵不過呼叫」錯誤

在我的頭文件:

template <typename V> 
void changeConfig(char option, vector <V> & arg_in){ 
     // .... 
    } 

在源文件中:

vector <int> p = {4}; 
changeConfig('w' ,p); 

這是我得到的錯誤:

/include/config.h:在成員函數「void Cfconfi克:: changeConfig(炭,性病::矢量< _RealType> &)[以V = INT]':

SRC/config_test.cpp:10:3​​8:從實例化這裏

/包括/配置。 H:68:25:錯誤:不對應的調用 '(INT_VECTOR {又名的std ::向量})(標準::矢量&)'

化妝:*** [featureconfig_test.o_dbg]錯誤1

我試過這個線程的建議,但沒有一個似乎工作。

C++ Templates Error: no matching function for call

任何幫助,將不勝感激。謝謝。

好的,我由我的代碼一小段賦予相同的錯誤:

#include <iostream> 
#include <stdio.h> 
#include <stdarg.h> 
#include <cstdio> 
#include <opencv2/opencv.hpp> 
#include <string.h> 
#include <math.h> 
#include <complex.h> 
#include <dirent.h> 
#include <fstream> 
#include <typeinfo> 
#include <unistd.h> 
#include <stdlib.h> 
#include <algorithm> 
#include <array> 
#include <initializer_list> 
#include <vector> 
#include <algorithm> 


using namespace std; 


template <typename V> 
void changeConfig(char option, vector <V> & arg_in){ 

    vector<int> window = {5}; 
    vector<double> scale = {3}; 

      switch(option){ 
        case 'w':           
          window(arg_in); 
          break; 
        case 's': 
          scale(arg_in); 
          break; 
      } 

    } 


int main(){ 

    vector <int> p = {3}; 
    changeConfig<int>('w', p); 

    return 0; 

} 

我使用編譯: 克++ -std =的C++ 0x test_template.cpp -o測試

這給了我此錯誤:

test_template.cpp:在函數「void changeConfig(炭,性病::矢量< _RealType> &)[瓦特第i V = INT] ':

test_template.cpp:45:33:從這裏實例

test_template.cpp:32:33:錯誤:不對應的調用'(標準::矢量)(STD ::矢量&)」

test_template.cpp:35:33:錯誤:不對應的調用 '(標準::矢量)(標準::矢量&)'

+0

您使用哪種編譯器和版本?這編譯好gcc 4.6。 – mfontanini

+1

更新您的編譯器:http://ideone.com/tBsACf – mfontanini

+0

gcc版本4.6。3(Ubuntu/Linaro 4.6.3-1ubuntu5) – mystique

回答

2

問題是與window(arg_in);scale(arg_in);。您正嘗試將std::vector稱爲另一個std::vector作爲參數。我想你正試圖將一個向量分配給另一個向量,所以只需使用賦值或swap即可。

vector<int> window = {5}; 
vector<int> scale = {3}; // Note I changed this from double to int 

     switch(option){ 
       case 'w':           
         window = arg_in; // Perhaps you meant arg_in = window 
         break; 
       case 's': 
         scale = arg_in; // Perhaps you meant arg_in = scale 
         break; 
     } 

} 

如果你想使用vector<double> scale,使用scale.assign(arg_in.begin(), arg_in.end());代替或周圍的其他方式arg_in.assign(scale.begin(), scale.end());

+0

它是如何在非推斷的情況下? – mfontanini

+0

我也嘗試過,因爲在另一個SO線程給出的建議..沒有工作:( – mystique

+0

'V'可以推斷沒有任何問題在這裏。 – mfontanini