简体   繁体   English

没有匹配的 function 调用 'std::vector::push_back(std::string&)'

[英]No matching function for call to ‘std::vector::push_back(std::string&)’

I was given a C++ assignment for my class to make a program that reads student data records from a user, and the program will then sort the keyed in data records into sorted order in terms of students' GPAs, or in terms of the alphabetical order of the students' last names, or doing both, depending on the user's option.我为我的 class 分配了 C++ 作业,以制作一个从用户读取学生数据记录的程序,然后该程序将键入的数据记录按照学生的 GPA 或字母顺序排序学生的姓氏,或两者都做,这取决于用户的选择。 The framework was given to me by my professor.这个框架是我的教授给我的。

I'm having difficulties with vectors and strings, and getting these to work in my given program.我在使用向量和字符串时遇到了困难,并且让它们在我给定的程序中工作。 I keep receiving the following errors:我不断收到以下错误:

no matching function for call to 'std::vector::push_back(std::string&)'没有匹配的 function 调用 'std::vector::push_back(std::string&)'

no matching function for call to 'std::vector::push_back(float&)'没有匹配的 function 调用 'std::vector::push_back(float&)'

I've included my full code below and would greatly appreciate if somebody could give it a look.我在下面包含了我的完整代码,如果有人可以看一下,我将不胜感激。 I understand that something needs to be changed for v.push_back();我知道需要为 v.push_back(); 更改某些内容; to work, but I'm not sure about the exact change I should make.工作,但我不确定我应该做的确切改变。 I'm very new to C++ and have a limited understanding of strings and vectors.我对 C++ 很陌生,对字符串和向量的理解有限。

Thank you so much in advance!非常感谢您!

    #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()
    {

    }

Your vector has a type std::vector<student> But you are trying to push a string instead:你的向量有一个类型std::vector<student>但是你试图推送一个字符串:

string names;
v.push_back(names);

You probably meant v.push_back(student{names, 0});您可能的意思是v.push_back(student{names, 0});

This however opens another issue with pushing the GPA.然而,这开启了推动 GPA 的另一个问题。 Here you should just iterate over existing elements:在这里,您应该只迭代现有元素:

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

I would also advise you to use emplace_back instead of push_back as a more efficient alternative:我还建议您使用emplace_back而不是push_back作为更有效的选择:

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

you can use vector::push_back requires the template type of your vector in you case你可以使用 vector::push_back 在你的情况下需要你的向量的模板类型

vector<student>::push_back(student);

the code you should use is something like this你应该使用的代码是这样的

v.push_back(student(constructor arguments))

or或者

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

i think the vector v is type student, but not string or float, thus it occurs error.我认为向量 v 是学生类型,但不是字符串或浮点数,因此会发生错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back 没有用于调用 &#39;std::vector 的匹配函数<node*> ::push_back(节点*&amp;)&#39; - no matching function for call to ‘std::vector<node*>::push_back(Node*&)’ 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 填充向量的向量时出错:没有匹配的函数来调用std :: basic_string :: push_back - Error populating a vector of vectors: no matching function to call for std::basic_string::push_back 没有匹配的 function 调用 'std::vector <std::vector<int> &gt;::push_back(int)' </std::vector<int> - no matching function for call to 'std::vector<std::vector<int> >::push_back(int)' 错误:没有匹配的 function 调用 'std::vector<float> ::push_back(std::vector<float> &amp;) 常量'</float></float> - error: no matching function for call to 'std::vector<float>::push_back(std::vector<float>&) const' 错误:没有匹配的函数调用&#39;std :: vector <std::vector<int> &gt; ::的push_back(标准::矢量 <std::__cxx11::basic_string<char> &gt;&)” - error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’ 错误:没有匹配函数来调用&#39;std :: vector <std::__cxx11::basic_string<char> &gt; ::的push_back(INT&)” - error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’ 标准::向量<std::string>和 push_back - std::vector<std::string> and push_back C++ - 在 foreach 中复制向量给出“没有匹配的函数来调用 std::vector<int> ::push_back(std::vector)<int> &amp;)” - C++ - copying vector in foreach gives "No matching function to call for std::vector<int>::push_back(std::vector<int>&)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM