| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2545 人关注过本帖
标题:一个C++的程序,实在看不出是哪里的错呢~
只看楼主 加入收藏
桃子0328
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-3-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
一个C++的程序,实在看不出是哪里的错呢~
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册


新手菜鸟求助~
一个 C++ 的小程序,主要功能是完成 Teacher 类对学生的信息管理。
但是实在看不懂到底哪里出错了,编译出了好多 errors
求大神带飞啊……
搜索更多相关主题的帖子: errors 信息 
2016-03-16 23:30
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:10 
为什么不复制贴出来,看图真心累
2016-03-16 23:35
桃子0328
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-3-16
收藏
得分:0 
不好意思啦,代码在下面~
// Manage students and teachers' information.
// features: set the students' scores and select the students
// whose scores are higher than 80 and print.
#include <assert.h>
#include <iostream>
#include <string>

using namespace std;

class Student;
// the teacher's information.
class Teacher {
 public:
  Teacher(string, const unsigned int); // 1 student at lease.
  ~Teacher(); // destructor.
  void insert_student(Student *); // insert a new student.
  void sort_students(); // sort the students' information.
  void print_students(); // print all the student's information.

 private:
  string name_;
  unsigned int maxn_; // the max number of the students.
  unsigned int stu_num_; // the number of the students.
  Student** pStu_; // the pointer point to the pointer of Student.
};
// Teacher's constructor.
Teacher::Teacher(string s, const unsigned int n) {
  name_ = s;
  maxn_ = n;
  stu_num_ = 0;
  pStu_ = new Student*[n]; // dynamically generate array.
}
// Teacher's destructor.
Teacher::~Teacher() {
  delete[] pStu_;
  pStu_ = NULL;
}
// insert a student.
void Teacher::insert_student(Student *p) {
  assert(stu_num_ < maxn_);
  *(pStu_ + stu_num_) = p; // point to the new student.
  stu_num_++; // the number of the students increase.
}
// sort the students' information using bubble sorting algorithms.
void Teacher::sort_students() {
  for(int i = 0; i != stu_num_ - 1; ++i) {
    for(int j = 0; j != stu_num_ - 1 - i; ++j) {
      if (pStu_[j]->score_ > pStu_[j + 1]->score_) {
        // swap the two pointer.
        Student *p;
        p = pStu_[j];
        pStu_[j] = pStu_[j + 1];
        pStu_[j + 1] = p;
      }
    }
  }
}
// print all the student's information.
void Teacher::print_students() {
  for(int i = 0; i != stu_num_; ++i) {
    cout << pStu_[i]->name_ << ": " << pStu_[i]->score_ << endl;
  }
}
// the student's information.
class Student {
 public:
  Student(string, double);
  friend void Teacher::sort_students();
  friend void Teacher::print_students();
  friend void select_student(Student*, double);

 private:
  string name_; // the student's name.
  double score_; // the student's score.
};
// Student's constructor.
Student::Student(string n, unsigned double s) {
  name_ = n;
  score_ = s;
}
// select the students whose scores are higher than d
// and print them.
void select_student(Student* s, double d) {
  if (s->score_ >= 80.0) {
    cout << s->name_ << ": " << s->score_ << endl;
  }
}
// test.
int main() {
  Teacher t1("Mr.Wang", 3);
  Student s1("student1", 90.2) , s2("student2", 85.3), s3("student3", 45.3);
  t1.insert_student(&s1);
  t1.insert_student(&s2);
  t1.insert_student(&s3);
  t1.sort_students();
  t1.print_students();
  return 0;
}
2016-03-16 23:38
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:0 
程序代码:
//我没改你的任何东西,只是调换了下代码块出现的次序。
//你图中出现红点的原因是“类或结构体的前向声明只能用来定义指针对象,因为编译到这里时还没有发现定义,不知道该类或者结构的内部成员,没有办法具体的构造一个对象,所以会报错。”
//要会用百度呀
#include <assert.h>

#include <iostream>
#include <string>

using namespace std;

class Student;
class Teacher;
// the teacher's information.
class Teacher {

 public:
  Teacher(string, const unsigned int); // 1 student at lease.
  ~Teacher(); // destructor.
  void insert_student(Student *); // insert a new student.
  void sort_students(); // sort the students' information.
  void print_students(); // print all the student's information.


 private:
  string name_;
  unsigned int maxn_; // the max number of the students.
  unsigned int stu_num_; // the number of the students.
  Student** pStu_; // the pointer point to the pointer of Student.
};
// the student's information.
class Student {

 public:
  Student(string, double);
  friend void Teacher::sort_students();
  friend void Teacher::print_students();
  friend void select_student(Student*, double);


 private:
  string name_; // the student's name.
  double score_; // the student's score.
};

// Teacher's constructor.
Teacher::Teacher(string s, const unsigned int n) {
  name_ = s;
  maxn_ = n;
  stu_num_ = 0;
  pStu_ = new Student*[n]; // dynamically generate array.
}
// Teacher's destructor.
Teacher::~Teacher() {
  delete[] pStu_;
  pStu_ = NULL;
}
// insert a student.
void Teacher::insert_student(Student *p) {
  assert(stu_num_ < maxn_);
  *(pStu_ + stu_num_) = p; // point to the new student.
  stu_num_++; // the number of the students increase.
}
// sort the students' information using bubble sorting algorithms.
void Teacher::sort_students() {
  for(int i = 0; i != stu_num_ - 1; ++i) {
    for(int j = 0; j != stu_num_ - 1 - i; ++j) {
      if (pStu_[j]->score_ > pStu_[j + 1]->score_) {
        // swap the two pointer.
        Student *p;
        p = pStu_[j];
        pStu_[j] = pStu_[j + 1];
        pStu_[j + 1] = p;
      }
    }
  }
}
// print all the student's information.
void Teacher::print_students() {
  for(int i = 0; i != stu_num_; ++i) {
    cout << pStu_[i]->name_ << ": " << pStu_[i]->score_ << endl;
  }
}

// Student's constructor.
Student::Student(string n, double s) {
  name_ = n;
  score_ = s;
}
// select the students whose scores are higher than d
// and print them.
void select_student(Student* s, double d) {
  if (s->score_ >= 80.0) {
    cout << s->name_ << ": " << s->score_ << endl;
  }
}
// test.
int main() {
  Teacher t1("Mr.Wang", 3);
  Student s1("student1", 90.2) , s2("student2", 85.3), s3("student3", 45.3);
  t1.insert_student(&s1);
  t1.insert_student(&s2);
  t1.insert_student(&s3);
  t1.sort_students();
  t1.print_students();
  return 0;
}
2016-03-17 09:06
桃子0328
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-3-16
收藏
得分:0 
回复 4楼 wengbin
真的好了哎~~~非常感谢~~~给你 fafa 么么哒
2016-03-17 20:07
Susake
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:女儿国的隔壁
等 级:贵宾
威 望:23
帖 子:2288
专家分:6481
注 册:2012-12-14
收藏
得分:10 

仰望星空...........不忘初心!
2016-03-17 20:25
快速回复:一个C++的程序,实在看不出是哪里的错呢~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.491861 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved