【问题标题】:Including H files in other programs x在其他程序中包含 H 文件 x
【发布时间】:2013-05-26 14:42:39
【问题描述】:

对于类,我创建了一个名为 time 的结构和 h.which 看起来像这样

struct Time{
    Time() : hours(0),minutes(0),seconds(0),ticks(0){}
    int hours, minutes, seconds, ticks;
};

Time convert (clock_t t, Time &time);
std::string hmst(Time &time);
std::string hmst(clock_t t);

后来我们的老师让我们制作了另一个程序,在这个程序中使用了相同的代码。而不是把它写在我包括在内。第一个问题是这是一个合法的#include(假设路径正确)

#include "../p*/*r*/0*/*s/02*/time.h"

第二个问题我需要在我当前正在处理的程序中使用转换功能。我该怎么做呢?我会按如下方式使用范围解析运算符吗

timeobeject=Time::convert(t,time); 

或者像这样

timeobject=convert(t,time);

【问题讨论】:

  • 为了更好地适应头文件或源文件的移动,不要在#include语句中放置路径。将路径作为搜索目录提供给编译器 (-I) 或构建系统/IDE。

标签: c++ include scope header-files include-path


【解决方案1】:

第一个问题是这是一个合法的#include(假设路径是正确的)

#include "../p*/r/0*/s/02/time.h"

是的。但请记住要与当前项目一起编译它的实现文件。或者您可以直接链接到目标文件。

timeobeject=时间::convert(t,time);

错了。 convert 函数未包含在任何命名空间中。你必须像这样直接调用它 -

timeobeject=convert(t,time); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多