的push_back()不工作的自定义数据类型(模板类)

问题描述:

显然的push_back()不工作我的自定义数据类T.在编译时,我得到以下错误:的push_back()不工作的自定义数据类型(模板类)

error: no matching function for call to ‘Vector::push_back(int&)’

有人能向我解释为什么是这样?谢谢。

#include <std_lib_facilities> 
#include <numeric> 
#include <vector> 
#include <string> 

// vector<int> userin;                                               
// int total;                                                  
// bool success;                                                 

class T 
{ 
public: 
    void computeSum(vector<T> userin, int sumamount, T& total, bool& success); 
    void getData(vector<T> userin); 
}; 

template <class T> 
void computeSum(vector<T> userin, int sumamount, T& total, bool& success) 
{ 

    if (sumamount < userin.size()){ 
     success = true; 
     int i = 0; 
     while (i<sumamount){ 
      total = total + userin[i]; 
      ++i; 
     } 
    } else { 
     success = false; 
     cerr << "You can not request to sum up more numbers than there are.\n"; 
    } 

} 

template <class> 
void getData(vector<T> userin) 
{ 
    cout << "Please insert the data:\n"; 
    int n; 

    do{ 
     cin >> n; 
     userin.push_back(n); 
    } while (n); 

    cout << "This vector has " << userin.size() << " numbers.\n"; 
} 

int helper() 
{ 
    cout << "Do you want help? "; 
    string help; 
    cin >> help; 
    if (help == "n" || help == "no"){ 
     return 0; 
    }else{ 
     cout << "Enter your data. Negative numbers will be added as 0. Ctrl-D to finish inputing values.\n"; 
    } 
} 

int main() 
{ 
    helper(); 
    getData(userin); 

    cout << "How many numbers would you like to sum?"; 
    int sumamount; 
    cin >> sumamount; 
    computeSum(userin, sumamount); 
    if (success = true) { 
     cout << "The sum is " << total << endl; 
    } else { 
     cerr << "Oops, an error has occured.\n"; 
    } 

    cout << endl; 
    return 0; 
} 
+0

您提供的代码有更严重的错误 - 没有'computeSum'函数取两个参数,例如,'template '是错误的。这真的是你得到的唯一错误吗?请提供您真正使用的代码 - 不要重新输入错误,复制并粘贴它们。此代码中没有大写的Vector类。 – bdonlan

+0

嘿,我已经跟在这里:http://*.com/questions/7657825/c-error-calling-template-functions –

以外的一些悍然进攻的问题(例如,它应该是template <class T>,不template<class>),真正的问题是,矢量希望你推回T类型的对象。它看起来像你正在阅读类型int和推。尝试:

template <class> 
void getData(vector<T> userin) 
{ 
    cout << "Please insert the data:\n"; 
    T n; 

    do{ 
     cin >> n; 
     userin.push_back(n); 
    } while (n); 

    cout << "This vector has " << userin.size() << " numbers.\n"; 
} 

的问题是这一行:

userin.push_back(n); 

其中n是一个int。 push_back期待T类型的东西。

我也不确定类T的点在这种情况下。