Compiler error while calling template functions

匆匆过客 提交于 2019-12-13 21:28:31

问题


I'm getting the following error upon compiling:

     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

Am I simply not calling my template

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

回答1:


Your function is declared and defined as:

void getData(Vector&, int&)

You are calling it as:

getData();

Clearly, the compiler cannot find the function which takes no parameteres and hence the no mathching function error.

Same is the case for computeSum().

There are a host of other errors as well like success and total are two variables which are being accessed in main but not declared anywhere inside the main.




回答2:


You are forgetting to pass the parameters to your functions e.g., :

void computeSum(vector<T> data, int n, T &out, bool &success)

computeSum();  

Apparently the function signatures do not match. Also your class T is not declared as a template class. I think this is what you originally intended. The functions computeSum and getData do not implement the public member functions of the class.



来源:https://stackoverflow.com/questions/7657825/compiler-error-while-calling-template-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!