开发者

How does one implement a function from a namespace?

Essentially, this is my source code.

namespace name {  
    int func (void);  
}

int main (voi开发者_如何学God) {  
    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
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜