【发布时间】:2021-02-24 13:14:09
【问题描述】:
所以我正在学习课程并且我正在制作一个打印用户输入数据的程序,但是我似乎无法克服这个编译器错误。该程序的要点是获取学生用户数据并输出。
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student();
~Student();
void SetName(string studentname); //mutator for name
void SetId(int studentid); //mutator for id
void SetGender(string studentgender); //mutator for gender
void SetEthnicity(string studentethnicity); // mutator for ethnicity
void SetMajor(string studentmajor); //mutator for major
void SetMinor(string studentminor); //mutator for minor
void SetGPA(float studentgpa); // mutator for gpa
string GetName(); //accessor for name
int GetId(); //accessor for id
string GetGender(); //accesor for gender
string GetEthnicity(); // accessor for ethnicity
string GetMajor(); // accessor for major
string GetMinor(); //accessor for minor
float GetGPA(); // accessor for GPA
void PrintInfo(void); // print student information
private:
string Name;
int id_number;
string gender;
string ethnicity;
string major;
string minor;
float gpa;
};
Student::Student()
{
Name = " ";
id_number = 0;
gender = " ";
ethnicity = " ";
major = " ";
minor = "None";
gpa = 0;
}
//mutator functions
void Student::SetName(string studentname)
{
Name = studentname;
};
void Student::SetId(int studentid)
{
id_number = studentid;
}
void Student::SetGender(string studentgender)
{
gender = studentgender;
}
void Student::SetEthnicity(string studentethnicity)
{
ethnicity = studentethnicity;
}
void Student::SetMajor(string studentmajor)
{
major = studentmajor;
}
void Student::SetMinor(string studentminor)
{
minor = studentminor;
}
void Student::SetGPA(float studentgpa)
{
gpa = studentgpa;
}
//accessor functions
string Student::GetName()
{
return Name;
}
int Student::GetId()
{
return id_number;
}
string Student::GetGender()
{
return gender;
}
string Student::GetEthnicity()
{
return ethnicity;
}
string Student::GetMajor()
{
return major;
}
string Student::GetMinor()
{
return minor;
}
float Student::GetGPA()
{
return gpa;
}
void Student::PrintInfo(void)
{
cout<<"Name: "<<myStudent.GetName()<<endl;
cout<<"ID #: "<<myStudent.GetId()<<endl;
cout<<"Gender: "<<myStudent.GetGender()<<endl;
cout<<"Ethnicity: "<<myStudent.GetEthnicity()<<endl;
cout<<"Major: "<<myStudent.GetMajor()<<endl;
cout<<"Minor: "<<myStudent.GetMinor()<<endl;
cout<<"GPA: "<<myStudent.GetGPA()<<endl;
}
int main()
{
int* studentarray;
studentarray = new int[5];
Student myStudent;
string names;
int ids;
string genders;
string ethnicities;
string majors;
string minors;
float gpas;
cout<<"Student Information."<<endl;
cout<<"Name: "<<endl;
getline (cin, names);
cout<<"ID Number: "<<endl;
cin>> ids;
cout<<"Gender: "<<endl;
getline(cin, genders);
cout<<"Ethnicity: "<<endl;
getline(cin, ethnicities);
cout<<"Major: "<<endl;
getline(cin, majors);
cout<<"Minor: "<<endl;
getline(cin, minors);
cout<<"Enter GPA: "<<endl;
cin>> gpas;
myStudent.SetName(names);
myStudent.SetId(ids);
myStudent.SetGender(genders);
myStudent.SetEthnicity(ethnicities);
myStudent.SetMajor(majors);
myStudent.SetMinor(minors);
myStudent.SetGPA(gpas);
myStudent.PrintInfo();
return 0;
}
那么这是我的编译器错误
在成员函数'void Student::PrintInfo()'中: 117:18:错误:未在此范围内声明“myStudent”
【问题讨论】:
-
这就是它所说的 -
myStudent不是Student::PrintInfo()里面的东西 -
您想完全按照答案所述解决此问题。此外,您可能还应该花一些时间阅读有关变量范围的信息。
myStudent存在于int main()内部,没有其他地方。