MichaelMicheal 2015-10-15 03:46 采纳率: 100%
浏览 2447
已采纳

求解关于C++中的条件编译#ifdef

IDE是codeblocks,源文件
第一个是头文件time.h,定义类

 // time.h
//#ifdef _TIME_H
#define _TIME_H
class Time
{
private:
    int hour;
    int minute;
    int second;
public:
    Time();
    void set_time(int h, int m, int s);
    void show_time();
};

//#endif

第二个是方法实现文件time_func.cpp


// time_func.cpp
#include <iostream>
#include "time.h"
Time::Time()
{
};
void Time::set_time(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
    return;
}
void Time::show_time()
{
    std::cout << "show time: " << hour << ":"
              << minute << ":" << second << std::endl;
    return;
}

第三个是main函数,main.cpp

 // the main function
#include <iostream>
#include "time.h"
using namespace std;
int main()
{
    Time t;
    t.set_time(19, 20, 59);
    t.show_time();


    return 0;
}

我把条件编译#ifdef和#endif注释掉就能编译通过,要不然就会出现编译失败:
||=== Build: Debug in test_class (compiler: GNU GCC Compiler) ===|
D:\C++\课后练习\test_class\time.h|2|error: unterminated #ifdef|
D:\C++\课后练习\test_class\main.cpp||In function 'int main()':|
D:\C++\课后练习\test_class\main.cpp|7|error: 'Time' was not declared in this scope|
D:\C++\课后练习\test_class\main.cpp|7|error: expected ';' before 't'|
D:\C++\课后练习\test_class\main.cpp|8|error: 't' was not declared in this scope|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

真心求解为何?

  • 写回答

2条回答 默认 最新

  • 纵横车 2015-10-15 03:58
    关注

    应该是#if**n**def和#endif吧。

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

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿