简体   繁体   中英

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. 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&)'

no matching function for call to '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(); 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.

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:

string names;
v.push_back(names);

You probably meant v.push_back(student{names, 0});

This however opens another issue with pushing the 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:

//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<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM