![]() |
#2
m21wo2010-10-23 10:36
怎么感觉你乱在贴啊!应该这么放!我也帮给你改了下!
![]() CourseClass.h #ifndef COURSE_H #define COURSE_H #include <vector> #include <string> using namespace std; class CCourse { public: CCourse(const string& courseName); string getCourseName(); void addStudent(const string &student); string getStudent(); int getNumberOfStudents(); private: string m_scourseName; vector<string> v_sstudent; int m_inumberOfStudents; }; #endif CourseClass.cpp #include "CourseClass.h" #include <iostream> using namespace std; CCourse::CCourse(const string& courseName) // 构造函数与类同名 { m_inumberOfStudents = 0; this->m_scourseName = courseName; } string CCourse::getCourseName() { return m_scourseName; } void CCourse::addStudent(const string &student) { v_sstudent.push_back(student); m_inumberOfStudents++; } string CCourse::getStudent() { return *v_sstudent.begin(); // v_sstudent.begin()返回的是地址 } int CCourse::getNumberOfStudents() { return m_inumberOfStudents; } void main() { CCourse course("Database"); string stuName; bool flag = 1; while(flag) { cout<<"请输入添加学生的姓名:"<<endl; cin>>stuName; // 怎么还在输入前加类型 course.addStudent(stuName); cout<<"继续加入学生? 是(1) 否(0)"<<endl; cin>>flag; } cout<<"本课堂的学生为"<<course.getCourseName()<<endl; cout<<"本课堂共有学生的人数为"<<course.getNumberOfStudents()<<endl; cout<<"检测通过"<<endl; } |

#ifndef COURSE_H
#define COURSE_H
#include <vector>
#include <string>
using namespace std;
class CCourse
{
public:
Course(const string &courseName);
string getCourseName();
void addStudent(const string &student);
string *getStudent();
int getNumberOfStudents();
private:
string m_scourseName;
vector<string> v_sstudent;
int m_inumberOfStudents;
};
#endif#include "CourseClass.h"
#include <iostream>
using namespace std;
CCourse::Course(const string &courseName)
{
m_inumberOfStudents = 0;
this->m_scourseName = courseName;
}
string CCourse::getCourseName()
{
return m_scourseName;
}
void CCourse::addStudent(const string &student)
{
v_sstudent.push_back(student);
m_inumberOfStudents++;
}
string *CCourse::getStudent()
{
return v_sstudent.begin();
}
int CCourse::getNumberOfStudents()
{
return m_inumberOfStudents;
}#include "CourseClass.h"
#include <iostream>
using namespace std;
void main()
{
CCourse course("Database");
string stuName;
bool flag = 1;
while(flag)
{
cout<<"请输入添加学生的姓名:"<<endl;
cin>>string stuName;
course.addStudent(stuName);
cout<<"继续加入学生? 是(1) 否(0)"<<endl;
cin>>flag;
}
cout<<"本课堂的学生为"<<course.getCourseName()<<endl;
cout<<"本课堂共有学生的人数为"<<course.getNumberOfStudents()<<endl;
cout<<"检测通过"<<endl;
}
系统总是提示一些莫名其妙的错误#define COURSE_H
#include <vector>
#include <string>
using namespace std;
class CCourse
{
public:
Course(const string &courseName);
string getCourseName();
void addStudent(const string &student);
string *getStudent();
int getNumberOfStudents();
private:
string m_scourseName;
vector<string> v_sstudent;
int m_inumberOfStudents;
};
#endif#include "CourseClass.h"
#include <iostream>
using namespace std;
CCourse::Course(const string &courseName)
{
m_inumberOfStudents = 0;
this->m_scourseName = courseName;
}
string CCourse::getCourseName()
{
return m_scourseName;
}
void CCourse::addStudent(const string &student)
{
v_sstudent.push_back(student);
m_inumberOfStudents++;
}
string *CCourse::getStudent()
{
return v_sstudent.begin();
}
int CCourse::getNumberOfStudents()
{
return m_inumberOfStudents;
}#include "CourseClass.h"
#include <iostream>
using namespace std;
void main()
{
CCourse course("Database");
string stuName;
bool flag = 1;
while(flag)
{
cout<<"请输入添加学生的姓名:"<<endl;
cin>>string stuName;
course.addStudent(stuName);
cout<<"继续加入学生? 是(1) 否(0)"<<endl;
cin>>flag;
}
cout<<"本课堂的学生为"<<course.getCourseName()<<endl;
cout<<"本课堂共有学生的人数为"<<course.getNumberOfStudents()<<endl;
cout<<"检测通过"<<endl;
}
error C2664: '__thiscall CCourse::CCourse(const class CCourse &)' : cannot convert parameter 1 from 'char [9]' to 'const class CCourse &'
Reason: cannot convert from 'char [9]' to 'const class CCourse'
error C2275:'string' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\xstring(612) : see declaration of 'string'