"No Matching Function for call"错误

"No Matching Function for call" Error

本文关键字:call 错误 for Function No Matching      更新时间:2023-10-16

我正在做这个项目,我必须将文件中的输入放入向量中,然后将该向量分解并将其元素发送到类中。我正在尝试构建班级人员的测试,但是当我尝试这样做时,我遇到了此错误。

错误:

C:UsersEricDropboxCSE 2122 - C++Project FilesHomework09main.cpp:57: error: no matching function for call to `person::person(int, std::vector<int, std::allocator<int> >&)'
C:UsersEricDropboxCSE 2122 - C++Project FilesHomework09main.cpp:9: note: candidates are: person::person(const person&)
C:UsersEricDropboxCSE 2122 - C++Project FilesHomework09main.cpp:17: note:                 person::person(int, std::vector<float, std::allocator<float> >)

主.cpp

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
/* Person Class */
class person
{
    public:
        int id;
        vector <float> scores;
        float averageScore;
    /* Person Constructor */
    person(int _id, vector<float> _scores)
    {
    id = _id;
    scores = _scores;
    int total;
    for(unsigned int i = 0; i < _scores.size(); i++)
    {
        total += _scores[i];
    }
    averageScore = (total / _scores.size());
    }
};
/* Function Prototypes */
string getFileName();
void readFile(string fileName, vector <int> &tempVec);
/* Main */
int main()
{
    vector <int> tempVec;
    vector <int> tempVec2;
    tempVec2.push_back(56);
    tempVec2.push_back(98);
    tempVec2.push_back(78);
    tempVec2.push_back(89);
    int personCount = 0;
    vector <person> personVector;
    string fileName = getFileName();
    readFile(fileName, tempVec);
    personCount = (tempVec.size())/(4);
    person eric = person(110, tempVec2);

}
/* getFileName Function */
string getFileName()
{
    string fileName;
    cout << "Enter the file you wish to read from: ";
    cin >> fileName;
    return fileName;
}
/* readFile Function */
void readFile(string fileName, vector <int> &tempVec)
{
    float x = 0;
    ifstream fin;
    fin.open(fileName.c_str(), ios::in);
    if(!fin.is_open())
    {
        perror(fileName.c_str());
        exit(10);
    }
    while(!fin.fail())
    {
        fin >> x;
        tempVec.push_back(x);
    }
    fin.close();
}

有什么想法吗?

您的person构造函数采用 float s 的vector,并且您正在尝试将tempVec2传递到其中,这是 int s 的vector