Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one implement a function from a namespace?

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.

like image 671
user569363 Avatar asked Apr 09 '26 18:04

user569363


2 Answers

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
}
like image 72
Mike Seymour Avatar answered Apr 11 '26 09:04

Mike Seymour


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;  
}
like image 21
Prasoon Saurav Avatar answered Apr 11 '26 09:04

Prasoon Saurav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!