【问题标题】:PrintInfo() Giving Me A Hard TimePrintInfo() 让我很难受
【发布时间】: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() 内部,没有其他地方。

标签: c++ class


【解决方案1】:

myStudentStudent::PrintInfo() 中不存在。

PrintInfo()实际上是一个成员函数,所以它应该看起来很简单。

void Student::PrintInfo(void)
{
    cout<<"Name: "<< GetName()<<endl;
    cout<<"ID #: "<< GetId()<<endl;
    cout<<"Gender: "<< GetGender()<<endl;
    cout<<"Ethnicity: "<< GetEthnicity()<<endl;
    cout<<"Major: "<< GetMajor()<<endl;
    cout<<"Minor: "<< GetMinor()<<endl;
    cout<<"GPA: "<< GetGPA()<<endl;
}

这个更好(编辑:更好的在这种情况下。如果 getter 方法要执行比检索值更复杂的任务,你会使用它们):

void Student::PrintInfo(void)
{
    cout<<"Name: "<< Name<<endl;
    cout<<"ID #: "<< id_number<<endl;
    cout<<"Gender: "<< gender<<endl;
    cout<<"Ethnicity: "<< ethnicity<<endl;
    cout<<"Major: "<< major<<endl;
    cout<<"Minor: "<< minor<<endl;
    cout<<"GPA: "<< gpa<<endl;
}

或者,为了清楚起见,您可能更喜欢这种格式:

void Student::PrintInfo(void)
{
    cout<<"Name: "<< this->Name<<endl;
    cout<<"ID #: "<< this->id_number<<endl;
    cout<<"Gender: "<< this->gender<<endl;
    cout<<"Ethnicity: "<< this->ethnicity<<endl;
    cout<<"Major: "<< this->major<<endl;
    cout<<"Minor: "<< this->minor<<endl;
    cout<<"GPA: "<< this->gpa<<endl;
}

作为旁注,您应该指定您的 getter 方法(以及 PrintInfo)不修改对象。

int GetWhatever() const {} // <-- The const keyword right there

【讨论】:

  • 不使用 getter 不一定“更好”。考虑例如gender 在内部是枚举的可能性,而GetGender 可以返回一个字符串
猜你喜欢
  • 1970-01-01
  • 2020-04-05
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2011-09-17
相关资源
最近更新 更多