过去几个小时我一直在使用此代码,似乎无法解决错误可能是什么。我创建了成员函数,我确信它们在main中被正确调用。在将每个变量用作main中调用的类函数的参数之前,错误消息是“错误:'变量'之前的预期主表达式”。
class Name_pairs
{
public:
Name_pairs();
void read_names(string nameOfPerson, vector <string> names);
void read_ages(vector <double> ages, double ageOfPerson, vector <string>& names);
void print();
private:
string nameOfPerson;
double ageOfPerson;
vector <string> names;
vector <double> ages;
};`
Name_pairs::Name_pairs()
{
}
Name_pairs::read_names(nameOfPerson, names)
{
while(true)
{
cout << "Please enter a name: (enter q to end) " << endl;
cin >> nameOfPerson;
if(nameOfPerson == q)
{
names.push_back(nameOfPerson);
break;
}else{
names.push_back(nameOfPerson);
}
}
}
Name_pairs::read_ages(ages,ageOfPerson, names)
{
int z =0;
while(true)
{
cout << "Please enter " << names[a] << "'s age: "
cin >> ageOfPerson;
ages.push_back(ageOfPerson);
z++;
if(ages.size()== names.size() )
{
break;
}
}
}
And then where the problem occurs is in the main function.
int main()
{
Name_pairs identifier;
identifier.read_names(string nameOfPerson, vector <string> names);
identifier.read_ages(vector <double> ages, double ageOfPerson, vector <string> names);
return 0;
}
任何帮助将不胜感激
答案 0 :(得分:0)
您的代码中存在许多错误。最明显的是Name_pairs::read_names
:
void Name_pairs::read_names(string nameOfPerson, vector <string> names) {
// ...
}
并且参数应该通过引用传递,正如@Neil Kirk正确注意到的那样。
read_ages
相同。
此外,您不应该在对这些方法的调用中指定类型:
string nameOfPerson;
vector <string> names;
identifier.read_names(nameOfPerson, names);