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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?