筱雲 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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?