我似乎无法弄清楚我正在尝试构建的这个 GPA 计算器程序发生了什么。问题出在 Student
类的 printTranscript
方法中。该方法调用同一个类的getGPA
方法,它返回一个double,输出的是一些海量数字而不是一个标准的GPA。
主要.cpp:
#include <iostream>
#include "Student.cpp"
using namespace std;
int main(){
Student stud("Lebron", 23232);
stud.addCourse("Passing", 3, 'A');
stud.addCourse("Finals Record", 4, 'D');
stud.printTranscript();
return 0;
}
学生.h:
#include "Course.h"
#include <vector>
class Student{
private:
string name;
int studentID;
vector<Course> courses;
public:
Student(){
name = "No Name";
studentID = 0;
}
Student(string n, int ID){
name = n;
studentID = ID;
}
string getName(){
return name;
}
void setName(string n){
name = n;
}
int getID(){
return studentID;
}
void setID(int ID){
studentID = ID;
}
void addCourse(string, int, char);
void addCourse(Course);
double getGPA();
void printTranscript();
};
学生.cpp:
#include "Student.h"
void Student::addCourse(string name, int credits, char grade){
courses.push_back(Course(name,credits,grade));
}
void Student::addCourse(Course c){
courses.push_back(c);
}
double Student::getGPA(){
double gradePoints, totalCredits;
for(int i = 0; i < courses.size(); i++){
if(courses[i].getGrade() == 'A'){
gradePoints += (4.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'B'){
gradePoints += (3.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'C'){
gradePoints += (2.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if (courses[i].getGrade() == 'D'){
gradePoints += (1.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
}
return (gradePoints / totalCredits);
}
void Student::printTranscript(){
cout << "Transcript for: " << name << endl;
cout << "============================" << endl;
for(int i = 0; i < courses.size(); i++){
cout << "Course: " << courses[i].getID() << endl;
cout << "Grade: " << courses[i].getGrade() << endl;
cout << endl;
}
cout << "Overall GPA: " << getGPA() << endl;
}
类(class).h:
#include <iostream>
#include <string>
using namespace std;
class Course{
private:
string courseID; // Course name
int numCredits; // Instructor
char letterGrade; // Textbook
public:
Course(string ID, int credits, char grade){ // Assign the course name.
courseID = ID;
numCredits = credits;
letterGrade = grade;
}
string getID(){
return courseID;
}
void setID(string ID){
courseID = ID;
}
int getCredits(){
return numCredits;
}
void setCredits(int credits){
numCredits = credits;
}
char getGrade(){
return letterGrade;
}
void setGrade(char grade){
letterGrade = grade;
}
};
抱歉代码太长了,但我似乎无法弄清楚为什么 main 中的输出会给我:
Transcript for: Lebron James
============================
Course: Passing
Grade: A
Course: Finals Record
Grade: D
Overall GPA: 2.2321e+230
如果我在主类中 cout stud.getGPA 作为一个单独的语句,它工作正常。当从同一类的另一个方法调用时,如何解释此处输出的大量数字,这是如何解决的?
抱歉,代码太长了,但我不想错过任何东西,因为我仍处于 C++ 的初级阶段。
最佳答案
来自 dcl.init/7 :
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type, constructors are considered. The applicable constructors are enumerated ([over.match.ctor]), and the best one for the initializer () is chosen through overload resolution. The constructor thus selected is called, with an empty argument list, to initialize the object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
你直接在做一些操作+=
:
totalCredits += courses[i].getCredits();
但是忘了初始化变量。不要假设所有内容最初都会设置为 零
。
因此,将 totalCredits
初始化为 0.0
。
添加-Wuninitialized
:
按照@1201ProgramAlarm 的建议,使用-Wuninitialized
标记未初始化的变量。
warning: 'totalCredits' may be used uninitialized in this function [-Wuninitialized]
如果您想将警告视为错误,只需添加标志 -Werror
关于c++ - 从同一类中的另一个方法调用方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49683486/