简体   繁体   English

在同一个名称空间中使用函数

[英]Using functions in the same namespace

Have a query on function using function in the same namespace. 使用同一名称空间中的函数查询函数。 How do I call the function? 如何调用该函数? It seems to keep on giving me error. 它似乎一直在给我错误。

Eg: 例如:

// 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
    }
}

The above code gives me error result, saying it can't find the helper1 function. 上面的代码给我错误的结果,说找不到helper1函数。 Is there anything I have done wrongly? 我做错了什么吗?

Thanks for your help! 谢谢你的帮助!

Are you sure you are asking the right quesiton? 您确定要问正确的问题吗? This is what I have, and it works fine: 这就是我所拥有的,并且工作正常:

// 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;
}

and the header file 和头文件

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

compiled using 使用编译

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

returns no warnings and yields 不返回警告和收益

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM