c++ - 调用模板函数时编译器错误

标签 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 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

我只是没有调用我的模板吗

#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;
}

最佳答案

您的函数声明并定义为:

void getData(Vector&, int&)

你称它为:

getData();

很明显,编译器找不到不带参数的函数,因此出现no mathching function错误。

computeSum() 也是如此。

还有许多其他错误,例如 successtotal 是在 main 中访问但未在任何地方声明的两个变量在 main 中。

关于c++ - 调用模板函数时编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7657825/

相关文章:

c++ - simple_type_specifier 不支持 nullptr_type

c++ - 我想在 C++ 中为 map<> 的给定 KEY 返回一个 VALUE。如果 KEY 不在 map<> 中,返回什么?

swift - “The compiler is unable to type-check this expression in reasonable time”提供一个简单的公式

C++ 模板 : Partial template Function Specialization in template class

c++ - 日历运行无限循环

c++ - 为什么运算符重载会出现自由错误?

C++11:计算可变函数参数类型

java - 类不是抽象的并且不会覆盖抽象方法 AWT Program

c++ - 链接时的waf : "undefined reference" error

c++ - (C++) 带有 SPI_SetMouse 的 SystemParametersInfo 似乎不会改变光标速度