筱雲 2015-11-09 16:55 采纳率: 100%
浏览 1579
已采纳

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

写了一个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;
}

  • 写回答

4条回答 默认 最新

  • ztenv 博客专家认证 2015-11-10 03:10
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿