我有幾個不同的類數據類型的向量,我試圖打印派生類的變量。如何訪問C++中派生類中的變量?
這裏是類
我已在圖實施的示意圖。我試圖在評分班的作業班打印成績。
的錯誤是在朋友的ostream &操作者< <(ostream的& OS,常量課程& DT)函數。
這裏是我的課
#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;
}
是的,但是錯誤信息本身是什麼? – Snappawapa
錯誤:'Grading'中沒有名爲'letter_grade'的成員 – Pete
這意味着clas'Grading'沒有任何名爲'letter_grade'的東西,它看着你的代碼是真的。你正試圖訪問'grading'中的東西 – Snappawapa