C++ 调用模板函数时出现编译器错误
我在编译时遇到以下错误:C++ 调用模板函数时出现编译器错误,c++,templates,compiler-errors,custom-data-type,C++,Templates,Compiler Errors,Custom Data Type,我在编译时遇到以下错误: Compilation started at Wed Oct 5 03:05:32 |make -k proj1 |g++ proj1.cc -o proj1 |proj1.cc: In function ‘int main()’: |proj1.cc:75:13: error: no matching function for call to ‘getData()’ |proj1.cc:75:13: note: candidate i
Compilation started at Wed Oct 5 03:05:32
|make -k proj1
|g++ proj1.cc -o proj1
|proj1.cc: In function ‘int main()’:
|proj1.cc:75:13: error: no matching function for call to ‘getData()’
|proj1.cc:75:13: note: candidate is:
|proj1.cc:46:6: note: template<class T> void getData(Vector<T>&, int&)
|proj1.cc:80:16: error: no matching function for call to
‘computeSum()’
|proj1.cc:80:16: note: candidate is:
|proj1.cc:28:6: note: template<class T> void computeSum(Vector<T>,
int, T&, bool&)
|proj1.cc:83:9: error: ‘success’ was not declared in this scope
|proj1.cc:84:27: error: ‘total’ was not declared in this scope
make: *** [proj1] Error 1
Compilation exited abnormally with code 2 at Wed Oct 5 03:05:33
编译于10月5日星期三03:05:32开始
|make-k proj1
|g++proj1.cc-o proj1
|proj1.cc:在函数“int main()”中:
|proj1.cc:75:13:错误:调用“getData()”时没有匹配的函数
|项目J1.cc:75:13:注:候选人为:
|proj1.cc:46:6:注意:模板void getData(Vector&,int&)
|proj1.cc:80:16:错误:没有用于调用的匹配函数
“computeSum()”
|项目J1.cc:80:16:注:候选人为:
|项目1.cc:28:6:注:模板无效计算(向量,
int,T&,bool&)
|proj1.cc:83:9:错误:未在此作用域中声明“success”
|proj1.cc:84:27:错误:未在此作用域中声明“total”
制造:**[proj1]错误1
编译在星期三10月5日03:05:33异常退出,代码为2
我只是没有调用我的模板吗
#include <std_lib_facilities.h>
#include <vector>
#include <string>
#include <iostream>
class T
{
public:
void computeSum(vector<T> in, int n, T &out, bool &success);
void getData(vector<T> &data, int &howMany);
};
template <class T>
// void computeSum(vector<T> data, int howMany, T& out, bool& success)
void computeSum(vector<T> data, int n, T &out, bool &success)
{
if (n < data.size()){
success = true;
int i = 0;
while (i<n){
out = out + data[i];
++i;
}
} else {
success = false;
cerr << "You can not request to sum up more numbers than there are.\n";
}
}
template <class T>
void getData(vector<T> &data, int &howMany)
{
cout << "Please insert the data:\n";
T n;
do{
cin >> n;
data.push_back(n);
} while (n<howMany);
cout << "This vector has " << data.size() << " numbers.\n";
}
void offerHelp()
{
cout << "Do you want help? ";
string help;
cin >> help;
if (help == "n" || help == "no"){
cout << endl;
}else{
cout << "Enter your data. Negative numbers will be added as 0. Ctrl-D to finish inputing values.\n";
}
}
int main()
{
offerHelp();
getData();
cout << "How many numbers would you like to sum?";
int howMany;
cin >> howMany;
computeSum();
if (success = true) {
cout << "The sum is " << total << endl;
} else {
cerr << "Oops, an error has occured.\n";
}
cout << endl;
return 0;
}
#包括
#包括
#包括
#包括
T类
{
公众:
无效计算(向量输入、整数n、T和输出、布尔和成功);
void getData(向量和数据、整数和数量);
};
模板
//void computeSum(矢量数据、整数数量、T和out、布尔和成功)
无效计算(矢量数据、整数n、T和out、布尔和成功)
{
如果(n 而(i您的函数被声明和定义为:
void getData(向量&,整数&)
你称之为:
getData()
显然,编译器找不到不带参数的函数,因此没有匹配函数
错误
computeSum()
的情况也是如此
还有许多其他错误,如success
和total
是在main
中访问但未在main
中声明的两个变量。您的函数声明和定义如下:
void getData(向量&,整数&)
你称之为:
getData()
显然,编译器找不到不带参数的函数,因此没有匹配函数
错误
computeSum()
的情况也是如此
还有许多其他错误,如success
和total
是在main
中访问但未在main
中声明的两个变量。您忘记了将参数传递给函数,例如:
void computeSum(vector<T> data, int n, T &out, bool &success)
computeSum();
void computeSum(向量数据、int n、T&out、bool&success)
computeSum();
显然,函数签名不匹配。而且您的类T没有声明为模板类。我认为这是您最初的意图。函数computeSum和getData没有实现类的公共成员函数。您忘记了将参数传递给函数,例如:
void computeSum(vector<T> data, int n, T &out, bool &success)
computeSum();
void computeSum(向量数据、int n、T&out、bool&success)
computeSum();
显然,函数签名不匹配。而且您的类T没有声明为模板类。我认为这是您最初的意图。函数computeSum和getData没有实现该类的公共成员函数