在同一命名空间中使用函数

时间:2013-10-17 03:45:34

标签: c++ namespaces

在同一名称空间中使用函数查询函数。我该如何调用该功能?它似乎继续给我错误。

E.g:

// in the .h files
namespace helper{ 
    void helper1();
    void helper2();
}

// in the cpp files
namespace helper{
    void helper1() { 
      // blah blah blah
    }

    void helper2() {
      // blah blah blah
      helper1();    // return some results from helper1
    }
}

上面的代码给出了错误结果,说它无法找到helper1函数。有什么我做错了吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你确定你问的是正确的问题吗?这就是我所拥有的,它运作良好:

// this is helper.cpp
#include<iostream>

#include "helper.hpp"

namespace helper{
void helper1() { 
  std::cout<<"calling helper 1"<<std::endl;
}

void helper2() {
  std::cout<<"calling helper 2"<<std::endl;
  helper1(); 
}
}

int main() {
  helper::helper2();
  return 0;
}

和头文件

//this is helper.hpp
namespace helper{ 
void helper1();
void helper2();
}

使用

编译
g++ helper.cpp -ansi -Wall -Wextra -pedantic

不返回任何警告和产量

$ ./a.out
calling helper 2
calling helper 1