c++学习记录1 类的多文件操作时的一个小问题(已解决)

2019/4/6
学习记录1
第一次练习类的多文件操作,出现了一些错误
例如:error: macro names must be identifiers
错误:宏名必须是标识符

我在原代码中写了这样一条:
#ifndef 123_H
#define 123_H
将代码改成
#ifndef a123_H
#define a123_H
编译通过。

经过上网查证,和对普通变量命名时一样,变量名不能以数字开头。

同时,在写头文件时,#ifndef 会首先检验常量 a123_H 是否已经存在,不存在就立即定义该常量,然后再定义 #ifndef 和 #endif 之间的类,反之其中间的代码被忽略。

//student.h
#ifndef STUDENT_H
#define STUDENT_H

class Student{
    string name;
    int age;
    string num;
    float *grade;
    int subjectNum;
    int type;
public:
    Student(string name1 = "NULL", string num1 = "NULL")
    {
        name = name1;
        num = num1;
        subjectNum = 0;
        type = 1;
        cout << "第1种构造" << endl;
    }

    Student(string name1, int age1)
    {
        age = age1;
        name = name1;
        subjectNum = 0;
        type = 2;
        cout << "第2种构造" << endl;
    }
    Student(string name1, string num1, int subjectNum1)
    {
        cout << "第3种构造" << endl;
        name = name1;
        num = num1;
        subjectNum = subjectNum1;
        if(subjectNum > 0)
        grade = new float[subjectNum];
        for(int i = 0; i < subjectNum; i++)
        {
            cout << "学科" << i + 1 << "分数: ";
            cin >> grade[i];
        }
        type = 3;
    }
    ~Student()
    {
        if(subjectNum > 0)
            delete[] grade;
        cout << "析构" << endl;
    }
    void Print()
    {
        if(type == 1)
        {
            cout << "姓名: " << name << endl
                 << "学号: " << num << endl;
        }
        else if(type == 2)
        {
            cout << "姓名: " << name << endl
                 << "年龄: " << age << endl;
        }
        else if(type == 3)
        {
            cout << "姓名: " << name << endl
                 << "学号: " << num << endl;
            for(int i = 0; i < subjectNum; i++)
            {
                cout << "学科" << i + 1 << "分数: " << grade[i] << endl;
            }
        }
    }

};
#endif
//main.cpp
#include<iostream>
using namespace std;
#include "student.h"
int main()
{
    Student s1("Jack", "101");
    s1.Print();
    cout << "------------------" << '\n' <<endl;
    Student s2("Tom", 18);
    s2.Print();
    cout << "------------------" << '\n' <<endl;
    Student s3("Mary", "102", 3);
    s3.Print();
    cout << "------------------" << '\n' <<endl;
    return 0;
}

以上程序能够成功运行,但当我把头文件的代码分割成 “student.h” 和 ”student.cpp“ 后,即把原先写在 “student.h” 的类中中各函数的代码放在了 ”student.cpp“ 编译无法通过,根据教材应该是可通过的。

报错类型: undefined reference to `Student::Student’ 等等
此处存疑。

以下为测试用错误代码

//ceshi.h
#ifndef CESHI_H_INCLUDED
#define CESHI_H_INCLUDED
class ceshi{
    int a;
public:
    ceshi(int);
    ~ceshi();
    int geta();
};
#endif // CESHI_H_INCLUDED
//ceshi.cpp
#include<iostream>
using namespace std;
#include "ceshi.h"
void ceshi::seta(int b)
{
    a = b;
}
int ceshi::geta()
{
    return a;
}
//main.cpp
#include<iostream>
using namespace std;
#include "ceshi.h"
int main()
{
    ceshi a(10);
    cout << a.geta();
}

在这里插入图片描述

补充:原因是要把头文件分成.h和.cpp需要建立一个项目来使其相联系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值