Push_back() 不适用于自定义数据类型(模板类)

发布于 2024-12-08 02:40:36 字数 2490 浏览 0 评论 0原文

显然,push_back() 不适用于我的自定义数据类 T。在编译时,出现以下错误:

错误:没有匹配的函数可用于调用“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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-12-15 02:40:36

除了一些公然令人反感的问题(例如,它应该是 template,而不是 template),真正的问题是向量希望你推回对象类型为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";
}

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";
}
苏璃陌 2024-12-15 02:40:36

问题出在这一行:

userin.push_back(n);

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

我也不确定在这种情况下 T 类的意义是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文