简体   繁体   中英

define a function within a namespace

namespace n1 {
  namespace n2 {
    ...
    int myfunc(void)
      {
         return 1;
      }
    class myclass {
      ..
    };
  }
}

I thought it is possible to define a function this way, and access it both from 'myclass' and its derivatives. However gcc doesn't even want to compile this code:

multiple definition of `n1::n2::myfunc()'

This function is the only one here, what am I missing? Thanks.

您需要将函数标记为inline以避免破坏一个定义规则,或者将实现放置在.cpp文件中,并且仅将声明保留在标头中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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