开发者社区> 问答> 正文

Linux下编译C++文件出现问题

写了一个Course类,编译g++ -std=c++11 Course.cpp时出现提示
screenshot
刚看的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;
}

展开
收起
杨冬芳 2016-07-14 13:55:15 2347 0
1 条回答
写回答
取消 提交回答
  • IT从业

    编译成可执行文件需要链接main函数的,所以需要定义main

    2019-07-17 19:56:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载