How does one implement a function from a namespace?

白昼怎懂夜的黑 提交于 2019-12-12 15:43:40

问题


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.


回答1:


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
}



回答2:


You can't define a function inside a function in C++.

This works

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

    return 0;  
}


来源:https://stackoverflow.com/questions/5725426/how-does-one-implement-a-function-from-a-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!