【发布时间】:2015-07-20 17:14:07
【问题描述】:
我有几个不同类数据类型的向量,我正在尝试打印派生类变量。
这是类的示意图
我已经实现了图表。我正在尝试从评分班的作业班打印分数。
错误在friend ostream& operator
这是我的课
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iomanip>
using namespace std;
/* -------------------------------------------------------- */
/* ---------------------- Grading Class ------------------- */
/* -------------------------------------------------------- */
class Grading
{
public:
string name;
int percent;
void get_raw_score();
void get_adj_score();
};
/* -------------------------------------------------------- */
/* ---------------------- Assignment Class ---------------- */
/* -------------------------------------------------------- */
class Assignment : public Grading
{
protected:
int score;
};
/* -------------------------------------------------------- */
/* ---------------------- Exam Class ---------------------- */
/* -------------------------------------------------------- */
class Exam : public Grading
{
protected:
int score;
Exam(string n, int g, int s) {
name = n;
percent = g;
score = s;
}
};
/* -------------------------------------------------------- */
/* ------------------- Project Class ---------------------- */
/* -------------------------------------------------------- */
class Project : public Assignment
{
public:
string letter_grade;
Project(string n, int g, string l_g) {
name = n;
percent = g;
letter_grade = l_g;
}
};
/* -------------------------------------------------------- */
/* ---------------------- Quiz Class ---------------------- */
/* -------------------------------------------------------- */
class Quiz : public Exam
{
public:
string letter_grade;
Quiz(string n, int g, string l_g) : Exam(n, g, score)
{
name = n;
percent = g;
letter_grade = l_g;
}
};
/* -------------------------------------------------------- */
/* ---------------------- CourseWork class ---------------- */
/* -------------------------------------------------------- */
class CourseWork {
public:
CourseWork() {}
void push_back(Quiz * a) {
work.push_back(a);
}
void push_back(Exam * a) {
work.push_back(a);
}
void push_back(Project * a) {
work.push_back(a);
}
// print the data and sort by name
void sort_name() {
for (int i = 0; i < (int)work.size(); i++)
cout<< work.at(i)->name <<endl;
}
void sort_score() {
}
friend ostream& operator<<(ostream& os, const CourseWork& dt) {
cout << "Grading" << std::setw(20) << "Percentage" << std::setw(20) << "Raw-Score" << endl;
for (int i = 0; i < (int)dt.work.size(); i++) {
// cout << dt.work.at(i)->name << std::setw(20) << dt.work.at(i)->percent << dt.work.at(i)->score <<endl;
os << dt.work.at(i)->name << std::setw(20) << dt.work.at(i)->percent << dt.work.at(i)->letter_grade;
}
return os;
}
private:
vector<Grading*> work;
};
/* -------------------------------------------------------- */
/* ---------------------- MAIN ---------------------------- */
/* -------------------------------------------------------- */
int main () {
CourseWork c;
c.push_back(new Quiz("Quiz", 5, "B-"));
c.push_back(new Quiz("Quiz", 5, "C+"));
c.push_back(new Quiz("Quiz", 5, "A"));
// c.push_back(new Exam("Midterm", 10, 50));
// c.push_back(new Exam("Final", 30, 85.5));
// c.push_back(new Project("Project", 5, "A-"));
// c.push_back(new Project("Project", 15, "B-"));
// c.push_back(new Project("Project", 15, "B-"));
// c.push_back(new Project("Demo", 10, "C"));
cout << "** Showing populated data..." << endl;
cout << c << endl << endl;;
// c.sort_name();
// c.sort_score();
return 0;
}
【问题讨论】:
-
是的,但是错误信息本身是什么?
-
错误:“评分”中没有名为“letter_grade”的成员
-
这意味着类
Grading没有任何称为letter_grade的东西,这看你的代码是正确的。您正在尝试访问grading中不存在的内容 -
Grading 中确实没有名为 letter_grade 的成员。你为什么要访问它?