写了一个Course类,编译g++ -std=c++11 Course.cpp时出现提示
刚看的C++ Primer,小白,还请给位前辈指点一下,谢谢。
Course.h
#ifndef COURSE
#define COURSE
#include <string>
#include <iostream>
using namespace std;
class Course{
friend istream &read(istream &,Course &);
friend ostream &print(ostream &,const Course &);
friend class CourseManager;
public:
explicit Course(string n="");
explicit Course(istream &is);
string getName() const;
void setName(const string &name);
private:
unsigned creatID();
unsigned course_id;
string course_name;
static unsigned ID; //静态成员,目的是希望实例化一个对象时自动分配ID,在构造函数里调用creatID()
};
unsigned Course::ID = 0;
istream &read(istream &,Course &);
ostream &print(ostream &,const Course &);
#endif
Course.cpp
#include "Course.h"
#include <string>
#include <iostream>
using namespace std;
//构造函数定义
Course::Course(string n):course_name(n),course_id(creatID()) { }
Course::Course(istream &is){
read(is,*this);
}
//接口部分成员函数
string Course::getName() const{
return course_name;
}
void Course::setName(const string &name){
course_name = name;
}
//封装部分成员函数
unsigned Course::creatID(){
++ID;
return ID;
}
//定义类相关的非成员函数
istream &read(istream &is,Course &it){
is >> it.course_id >> it.course_name;
return is;
}
ostream &print(ostream &os,const Course &it){
os << it.course_id << " " << it.course_name;
return os;
}