【问题标题】:No matching function for call to ‘std::vector::push_back(std::string&)’没有匹配函数调用‘std::vector::push_back(std::string&)’
【发布时间】:2020-06-24 15:25:54
【问题描述】:

我的班级给我一个 C++ 作业,让我编写一个程序,该程序从用户那里读取学生数据记录,然后该程序会将键入的数据记录按照学生的 GPA 或按照排序顺序排序学生姓氏的字母顺序,或者两者都做,这取决于用户的选择。这个框架是我的教授给我的。

我在使用向量和字符串时遇到了困难,要让它们在我给定的程序中工作。我不断收到以下错误:

没有匹配函数调用‘std::vector::push_back(std::string&)’

没有匹配的函数调用'std::vector::push_back(float&)'

我在下面包含了我的完整代码,如果有人能看一下,我将不胜感激。我知道需要为 v.push_back(); 更改某些内容;工作,但我不确定我应该做的确切改变。我是 C++ 新手,对字符串和向量了解有限。

非常感谢您!

    #include <iostream>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <vector>

    using namespace std;

    struct student
    {
      string name;
      float gpa;
    };

    std::vector <student> v;

    void input_data(std::vector<student> &v);
    void output_data();
    void swap(student *std1, student *std2);
    void sort_gpa_asc();
    void sort_gpa_des();
    void sort_name();
    void show_data();
    int num;

    int main()
    {
      char option;
      v.push_back(student{"Dana", 3.6});

      input_data(v);

      cout << "Your options for sorting (key in 'G' to sort students' data in terms of gpa in" << endl
           << "both ascending and descending order; key in 'A' to sort students' data in terms of" << endl
           << "alphabetical order of students' last name; key in 'B' for both types of sorting): ";
      cin >> option;
      int num;
      if (option == 'G')
        {
            sort_gpa_asc();
            sort_gpa_des();
        }
      else if (option == 'A')
        {
            sort_name();
        }
    else if (option == 'B')
        {
            sort_gpa_asc();
            sort_gpa_des();
            sort_name();
        }
    else
                cout << "Incorrect option!" << endl;

      return 0;
    }

    void input_data(std::vector<student> &v)
    {
      string names;
      float grade_point_average;
      int num;
      v.push_back(student{"Dana", 3.6});

      cout << "Enter the number of students: ";
      cin >> num;

      for(int i=0; i < num; i++){
              cout << "Enter student's name (write the surname before the first name): ";
              cin.get();
              getline(cin, names);
              v.push_back(names);
            }

      for(int i=0; i < num; i++){
              cout << "Enter student's GPA: ";
              cin >> grade_point_average;
              v.push_back(grade_point_average);
            }

    for(int i=0; i < v.size(); i++){
           cout << v[i].gpa;
        }
            for(int i=0; i < v.size(); i++){
                 cout << v[i].name;
                }

    cin.get();
    cin.get();

    }


    void swap(student *std1, student *std2)
    {

    }

    void sort_gpa_asc()
    {

    }

    void sort_gpa_des()
    {

    }

    void sort_name()
    {

    }

    void show_data()
    {

    }

【问题讨论】:

    标签: c++ string vector


    【解决方案1】:

    你的向量有一个类型std::vector&lt;student&gt; 但是您正在尝试推送一个字符串:

    string names;
    v.push_back(names);
    

    你的意思可能是v.push_back(student{names, 0});

    然而,这带来了推动 GPA 的另一个问题。在这里,您应该只迭代现有元素:

    for(int i=0; i < num; i++){
        cout << "Enter student's GPA: ";
        cin >> grade_point_average;
        v[i].gpa = grade_point_average;
    }
    

    我还建议您使用emplace_back 而不是push_back 作为更有效的替代方案:

    //v.push_back(student{"Dana", 3.6});
    v.emplace_back("Dana", 3.6);
    

    【讨论】:

      【解决方案2】:

      您可以使用 vector::push_back 在您的情况下需要您的向量的模板类型

      vector<student>::push_back(student);
      

      你应该使用的代码是这样的

      v.push_back(student(constructor arguments))
      

      student s = {.name = "name", .gpa = x};
      v.push_back(s);
      

      【讨论】:

        【解决方案3】:

        我认为向量 v 是 student 类型,而不是 string 或 float,因此会发生错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-06
          • 1970-01-01
          • 2021-05-05
          • 1970-01-01
          • 2018-12-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多