我正在研究这个项目,我必须将文件中的输入转换为矢量,然后拆分该矢量并将其元素发送到类中。我正在尝试对班级人员进行测试,但是当我尝试这样做时,我遇到了这个错误。
错误:
C:\Users\Eric\Dropbox\CSE 2122 - C++\Project Files\Homework09\main.cpp:57: error: no matching function for call to `person::person(int, std::vector<int, std::allocator<int> >&)'
C:\Users\Eric\Dropbox\CSE 2122 - C++\Project Files\Homework09\main.cpp:9: note: candidates are: person::person(const person&)
C:\Users\Eric\Dropbox\CSE 2122 - C++\Project Files\Homework09\main.cpp:17: note: person::person(int, std::vector<float, std::allocator<float> >)
Main.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();
}
有什么想法吗?
答案 0 :(得分:2)
您的person
构造函数需要vector
float
秒,并且您正试图将tempVec2
传递给vector
int
{{1}} 1}} S上。