开发者

push_back() not working for custom data type (template class)

Apparently push_back() is not working for my custom data class T. On compilation I get the following error:

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

Could someone explain to me why that is? Thank you.

#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(vecto开发者_如何转开发r<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;
}


Outside some flagrantly offensive issues (e.g. it should be template <class T>, not template<class>), the real problem is that vector expects you to push back objects of type T. It looks like you are reading in with type int and pushing. Try:

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


The problem is this line:

userin.push_back(n);

where n is an int. push_back is expecting something of type T.

I'm also not sure what the point of class T is in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜