| re: namespace question/problem
"johny smith" <princetonharvard@charter.net> wrote in message
news:10fubd5kqg07b1b@corp.supernews.com...[color=blue]
> I was defining one of my own math functions for sin. So I thought I would
> create a unique name space. Then use that namespace in my program to call
> my custom sin math function. That way I was hoping that I would not call
> the math library math function but my own custom.
>
> But it did not seem to work. Here is what I did.
>
>
> in my.h file
>
> #include <cmath>[/color]
Why are you including this header? You're not referring to
anything declared by it.
[color=blue]
> namespace myMath {
>
> double sin( double ); // this is a declaration for my custom function
>
> }
>
> in my .cpp file
>
> #include "my.h"[/color]
If you want to call 'std::math' from this translation unit,
#include <cmath>
[color=blue]
>
> namespace myMath {
>
> double sin( double input ) // this is my custom math function
> definition
> {
> stdio::cout << "my custom math function\n" << std::endl;
>
> sin( input ); // this a call to the <cmath> library[/color]
No, it's not. It's a recursive invocation of 'myMath::sin()'
If you have <cmath> #included, you can call the standard 'sin()'
function with:
::std::sin(input);
[color=blue]
>
> }
> }
>
> using namespace myMath; // hopefully this will cause my custom sin[/color]
function[color=blue]
> to be called[/color]
It will, if it has been linked with the rest of your program.
[color=blue]
>
> int main()
> {
> // I was hoping this would call my math function sin not the <cmath>
> function since I am using the myMath namespace.[/color]
Yes, it should.
[color=blue]
> std::cout << "the value is " << sin(.3) << std::endl;
>
> return 0;
> }
>
>
> So, what am I doing wrong here?[/color]
See above.
-Mike |