筱雲 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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程