gpt4 book ai didi

c++ - 我正在尝试在C++上做傻瓜练习,但遇到错误,说 'GraduateStudent'类没有名为 'advisor'的任何字段

转载 作者:行者123 更新时间:2023-12-02 10:21:56 24 4
gpt4 key购买 nike

在尝试创建两个类并使用它时,我遇到一个错误,说我的“GraduateStudent类”没有对象的任何名为“advisor”的字段,其代码如下:请帮我解决它谢谢
该程序演示了两个对象的创建和使用,一个类为Student类,第二个为GraduateStudent。该程序的输出如下:

constructing student Cy N
6
Sense constructing student Matt Madox
constructing graduate student Matt Madox
2
adding grade to Cy N Sense
adding grade to Matt Madox Matt’s qualifier grade = 1.5
Press any key to continue . . .

码:
//inheritanceexample
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <strings.h>

using namespace std;
// Advisor empty class

class Advisor{};
class Student
{
public:
Student((char* pName="no name")
: average(0.0), SemesterHours(0)
{
strncpy(name, pName, MAXNAMESIZE);
name[MAXNAMESIZE -1] = '\0';
cout << "constructing object"
<< name
<< endl;
}
void addcourse(int Hours, float grade)
{
cout << "adding grade to " << name << average << endl;
average = ( SemesterHours * average + grade );
SemesterHours +=Hours;
average = average / SemesterHours;
}
int hours(){return SemesterHours;}
float gpa(){return average;}
}

void addCourse(int hours, float grade)
{
cout << "adding grade to " << name << average << endl;
average = ( SemesterHours * average + grade );
SemesterHours +=Hours;
average = average / SemesterHours;
}
int hours(){return SemesterHours;}
int gpa(){return average;}
protected:
char name[MAXNAMESIZE];
int SemesterHours;
float average;

};
// my sublass

class GraduateStudent : public Student
{
public:
GraduateStudent(char *pName, Advisor &adv, float qG = 0.0) : Student(pName), advisor(adv), qualifierGrade(qG)
{
advisor;
float qualifiergrade;
cout<< "constructing graduate students"
<<pName
<<endl;
}
};

int main(int nNumberofargs, char* PSZArgs[])
{
Advisor advisor;
// create two objects

Student llu("Cy N Sense");
GraduateStudent gs("Matt Madox", advisor, 1.5);
llu.addCourse(3, 2.5); gs.addCourse(3, 3.0);
// display the graduate student’s qualifier grade

cout << "Matt’s qualifier grade = "
<< gs.qualifier()
<< endl;
cout << "Matt’s qualifier grade = "
<< gs.qualifier()
<< endl;
// wait until user is ready before terminating program

syestem("PAUSE")
return 0;

};

最佳答案

“类'GraduateStudent'没有名为'advisor'的字段”

没错,您的类(class)(学生和研究生)都没有名为顾问的成员变量。

也许这段代码:

class GraduateStudent : public Student
{
public:
GraduateStudent(char *pName, Advisor &adv, float qG = 0.0) : Student(pName), advisor(adv), qualifierGrade(qG)
{
advisor;
float qualifiergrade;
cout<< "constructing graduate students"
<<pName
<<endl;
}
};

搞砸了,你想写这个:
class GraduateStudent : public Student
{
public:
Advisor advisor;
float qualifiergrade;
GraduateStudent(char *pName, Advisor &adv, float qG = 0.0) : Student(pName), advisor(adv), qualifierGrade(qG)
{

cout<< "constructing graduate students"
<<pName
<<endl;
}
};

关于c++ - 我正在尝试在C++上做傻瓜练习,但遇到错误,说 'GraduateStudent'类没有名为 'advisor'的任何字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59788662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com