所以基本上我的教授给了我们班级学生,并告诉我们实现所有功能并为函数编写一个main。我正在努力做到这一点,但我无法弄清楚出现的错误,在我的所有访问器函数中它告诉我nCredits,gpa和s都没有在范围内声明。我认为这与第二个构造函数有关,但我不确定究竟是什么问题。第二个构造函数和displayStRec函数也提供给了我们的类,我只是不知道如何使用成员函数中的变量重新命名这个程序。
#include <iostream>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits(int nCredits);
int getCredits();
void changeGPA(double gpa);
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student()
{
name = "No name yet";
GPA = 0;
credits = 0.0;
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits)
{
name = s;
GPA = gpa;
credits = nCredits;
cout << "Second constructor entered!" << endl;
}
void student::changeCredits(int nCredits){
cout << "Enter credits earned:" << endl;
cin >> nCredits;
}
int student::getCredits(){
return nCredits;
}
void student::changeGPA(double gpa){
cout << "Enter GPA: " << endl;
cin >> gpa;
}
double student::getGPA(){
return gpa;
}
void student::displayStRec()
{
cout << " Name: " << s <<endl;
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits(nCredits);
s2.getCredits();
s2.changeGPA(gpa);
s2.getGPA();
s2.displayStRec();
return 0;
}
答案 0 :(得分:1)
您已经使用了三个未声明的变量。我会一个接一个地打他们。
void student::displayStRec()
{
cout << " Name: " << s <<endl;
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
在此函数中,您尝试输出一些未在displayStRec范围内声明的变量s
。您在构造函数中使用s
来初始化名称,将其复制到成员变量name
。那么,s
现在已经消失了!但是name
在对象的生命周期中仍然存在。所以,只需使用name
。
void student::displayStRec()
{
cout << " Name: " << name <<endl; //Change it here
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
在您的主要功能中,您使用了两个未声明的变量。
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits(nCredits); //What nCredits?
s2.getCredits();
s2.changeGPA(gpa); //What gpa?
s2.getGPA();
s2.displayStRec();
return 0;
}
这导致了一个问题......你想要将他们的学分改为到?你想改变他们的gpa 到是什么?换句话说,你的程序不应该有输入参数吗?也许在你的任务的某个地方,你的教授告诉你要提供什么价值。您将需要某种常量,或者您需要从cin
收集用户输入。
void student::changeGPA(double gpa){
cout << "Enter GPA: " << endl;
cin >> gpa;
}
此函数将接受用户输入并更新变量gpa
,该变量在本地范围内声明并立即丢弃。不太有用。
此外,它接受输入值(double gpa
),但也收集来自cin
的输入。两个为什么两个?无法同时将其更新为两个值!
我的猜测是应该更新对象的GPA,这是在成员范围内声明的。像这样:
void student::changeGPA(){
cout << "Enter GPA: " << endl;
cin >> GPA;
}
或
void student::changeGPA(double gpa){
GPA = gpa;
}
以下其中一项必须如此:
答案 1 :(得分:0)
我已经完成并删除了我教授试图对我们施加压力的第二个构造函数,这个程序现在运行正常:
#include <iostream>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits();
int getCredits();
void changeGPA();
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student()
{
name = "No name yet";
GPA = 0;
credits = 0.0;
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits)
{
name = s;
GPA = gpa;
credits = nCredits;
cout << "Second constructor entered!" << endl;
}
void student::changeCredits(){
cout << "Enter credits earned: ";
cin >> credits;
}
int student::getCredits(){
return credits;
}
void student::changeGPA(){
cout << "Enter GPA: ";
cin >> GPA;
}
double student::getGPA(){
return GPA;
}
void student::displayStRec()
{
cout << " Name: " << name <<endl;
cout << " GPA: " << GPA <<endl;
cout << "Credits: " << credits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits();
s2.getCredits();
s2.changeGPA();
s2.getGPA();
s2.displayStRec();
return 0;
}
答案 2 :(得分:0)
这是一个建议的解决方案,但我不知道你老师的命令:
!:将字符串添加到包含
2:仅使用会员数据而非非会员
3:要么使changeCredits和changeGPA取0参数,要求输入或输入外部并传递给它。
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits();
int getCredits();
void changeGPA();
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student() :
name("No name yet"), GPA(0.0), credits(0)
{
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits) :
name(s), GPA(gpa), credits(nCredits)
{
cout << "Second constructor entered!" << endl;
}
void student::changeCredits()
{
cout << "Enter credits earned:" << endl;
cin >> credits;
cout << endl;
}
int student::getCredits()
{
return credits;
}
void student::changeGPA()
{
cout << "Enter GPA: " << endl;
cin >> GPA;
cout << endl;
}
double student::getGPA()
{
return GPA;
}
void student::displayStRec()
{
cout << " Name: " << name <<endl;
cout << " GPA: " << GPA <<endl;
cout << "Credits: " << credits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1, s2("John", 4.0, 42);
s2.changeCredits();
s2.getCredits();
s2.changeGPA();
s2.getGPA();
s2.displayStRec();
return 0;
}