【问题标题】:Header file for functions inside of a namespace?命名空间内函数的头文件?
【发布时间】:2009-06-15 04:44:42
【问题描述】:

在 C++ 中,我有一个文件 A.cpp,其中包含以下内容:

 namespace Foo {

     bool Bar() 
     { 
         return true; 
     }

 }

如何在 A.h 中声明这个函数?如何处理命名空间?

【问题讨论】:

  • 哎呀,我想知道答案是什么。 :)
  • 又一个 StackOverflow 模因正在制作中?

标签: c++ namespaces header


【解决方案1】:
namespace Foo {
  bool Bar();
}

【讨论】:

    【解决方案2】:
    namespace Foo {
        bool Bar();
    }
    

    【讨论】:

    • +1 表示字符与字符相同,与 John Dibling 的答案相同,时间戳相同(四舍五入)
    • 实际上并非如此。 sharptooth 在这两个额外的空格字符和一个换行符上花了 4 秒。 45wpm...弱。
    【解决方案3】:
    namespace Foo {
        bool Bar();
    }
    

    或者

    /* don't look -- I must have been dreaming or mis-remembering -- the following does NOT work */
    namespace Foo;
    bool Foo::Bar();
    

    【讨论】:

    • 您可以使用命名空间 Foo {} 在任意多个位置包装声明和定义。它基本上将“Foo ::”添加到括号内所有内容的名称前面。不在您命名的命名空间内的事物位于默认的全局命名空间中。
    • 第二个很好的例子。我从来没有考虑过这种方法。
    • 第二个很好的例子。可惜它没有编译。
    【解决方案4】:
    namespace Foo {
        bool Bar();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      相关资源
      最近更新 更多