如何从命名空间实现函数? [英] How does one implement a function from a namespace?

查看:117
本文介绍了如何从命名空间实现函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这是我的源代码。

Essentially, this is my source code.

namespace name {  
    int func (void);  
}

int main (void) {  
    name::int func (void) {  
        //body
    }  
    return 0;  
}

现在,我想将这个函数写入命名空间不同的地方。

Now, I want to write that function, declared int the namespace, in a different place.

推荐答案

您不能在类似的另一个函数中定义函数。有两个选项:

You can't define the function inside another function like that. There are two options:

重新打开命名空间,并在其中定义函数:

Reopen the namespace, and define the function inside it:

namespace name {
    int func() {
        // body
    }
}

在命名空间之外(以及任何函数或类定义之外),使用其完全限定名称定义它:

Outside the namespace (and also outside any function or class definition), define it using its fully-qualified name:

int name::func() {
    // body
}

这篇关于如何从命名空间实现函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆