Method definition calling a function with the same name

Discussion in 'C++' started by Spoon, Dec 29, 2006.

  1. Spoon

    Spoon Guest

    Hello,

    Consider the following code:

    int foo(int n) { return n+2; }

    class bar
    {
    public:
    int x;
    bar() { x = 42; }
    int foo() { return foo(x); }
    };

    int main()
    {
    bar baz;
    return baz.foo();
    }

    Apparently this doesn't work.

    test.cpp: In member function `int bar::foo()':
    test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    test.cpp:8: note: candidates are: int bar::foo()

    I was told that the original foo lives in the unnamed namespace, and I
    that I could refer to that function with ::foo

    int foo(int n) { return n+2; }

    class bar
    {
    public:
    int x;
    bar() { x = 42; }
    int foo() { return ::foo(x); }
    };

    int main()
    {
    bar baz;
    return baz.foo();
    }

    Is that how it's usually done? Are there other/better ways?

    Do I need to place my helper functions in a specific namespace?

    Regards.
    Spoon, Dec 29, 2006
    #1
    1. Advertising

  2. Try avoiding such situations in the first place! From the look of it,
    seems like u r trying to invoke a global function from inside a class
    member scope. If u are writer and consumer urself, ideally u shud be
    trying to bind the function declaration inside a suitable namespace /
    class rather than leaving it in the dirty hands of global namespace.

    Spoon wrote:
    > Hello,
    >
    > Consider the following code:
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Apparently this doesn't work.
    >
    > test.cpp: In member function `int bar::foo()':
    > test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    > test.cpp:8: note: candidates are: int bar::foo()
    >
    > I was told that the original foo lives in the unnamed namespace, and I
    > that I could refer to that function with ::foo
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return ::foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Is that how it's usually done? Are there other/better ways?
    >
    > Do I need to place my helper functions in a specific namespace?
    >
    > Regards.
    Sandip Shahane, Dec 29, 2006
    #2
    1. Advertising

  3. Spoon

    Guest

    On Dec 29, 3:24 pm, Spoon <> wrote:
    > Hello,
    >
    > Consider the following code:
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return foo(x); }
    >
    > };int main()
    > {
    > bar baz;
    > return baz.foo();
    >
    > }Apparently this doesn't work.
    >
    > test.cpp: In member function `int bar::foo()':
    > test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    > test.cpp:8: note: candidates are: int bar::foo()
    >
    > I was told that the original foo lives in the unnamed namespace, and I
    > that I could refer to that function with ::foo
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return ::foo(x); }
    >
    > };int main()
    > {
    > bar baz;
    > return baz.foo();
    >
    > }Is that how it's usually done? Are there other/better ways?
    >
    > Do I need to place my helper functions in a specific namespace?
    >
    > Regards.


    I would put this function in its own namespace, like

    #include <iostream>
    namespace foo
    {
    int foo(int n) { return n+2; }
    }

    class bar
    {
    public:
    int x;
    bar() { x = 42; }
    int foo() { return foo::foo(x); }

    };

    int main()
    {
    bar baz;
    std::cout << baz.foo() << std::endl;
    return 0;
    }
    , Dec 29, 2006
    #3
  4. Spoon wrote:
    ....
    > I was told that the original foo lives in the unnamed namespace, and I
    > that I could refer to that function with ::foo
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }


    ^^^^^^^^^^ get used to using the initializer syntax.

    > int foo() { return ::foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Is that how it's usually done? Are there other/better ways?
    >
    > Do I need to place my helper functions in a specific namespace?


    If the helper function is specific to a class, you can place it in the
    class.

    e.g.


    class bar
    {
    static int foo(int n) { return n+2; }
    public:
    int x;
    bar() : x(42) { }
    int foo() { return foo(x); }
    };
    Gianni Mariani, Dec 29, 2006
    #4
  5. Spoon

    Jim Langston Guest

    "Spoon" <> wrote in message
    news:4595253f$0$321$...
    > Hello,
    >
    > Consider the following code:
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Apparently this doesn't work.
    >
    > test.cpp: In member function `int bar::foo()':
    > test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    > test.cpp:8: note: candidates are: int bar::foo()
    >
    > I was told that the original foo lives in the unnamed namespace, and I
    > that I could refer to that function with ::foo
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return ::foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Is that how it's usually done? Are there other/better ways?


    Yes, if the global function is in the unnamed namespace.

    > Do I need to place my helper functions in a specific namespace?
    >
    > Regards.
    Jim Langston, Dec 29, 2006
    #5
  6. Spoon

    Ivan Novick Guest

    Spoon wrote:
    > Hello,
    >
    > Consider the following code:
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Apparently this doesn't work.


    Ok, but lets be clear. AFAIK, the reason it doesn't work is not
    because it is a global function in the unnamed namespace. It is
    because it is a global function in the unnamed namespace with the same
    name as a function in your class. If you had a global function that
    did not have the same name as a name in your class then you could refer
    to it without using the :: qualifier.

    Another solution, not mentioned yet, is to put both your class and your
    utility functions in the same namespace and then you can refer to the
    utility functions without a qualifier. Again if there is a name
    conflict you will need to qualify the function name but at least your
    function would not be in the global namespace and in normal cases you
    would not need to qualify it.
    ---
    Ivan
    http://www.0x4849.net
    Ivan Novick, Dec 30, 2006
    #6
  7. Spoon

    Spoon Guest

    Spoon wrote:

    > Consider the following code:
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Apparently this doesn't work.
    >
    > test.cpp: In member function `int bar::foo()':
    > test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    > test.cpp:8: note: candidates are: int bar::foo()
    >
    > I was told that the original foo lives in the unnamed namespace, and I
    > that I could refer to that function with ::foo
    >
    > int foo(int n) { return n+2; }
    >
    > class bar
    > {
    > public:
    > int x;
    > bar() { x = 42; }
    > int foo() { return ::foo(x); }
    > };
    >
    > int main()
    > {
    > bar baz;
    > return baz.foo();
    > }
    >
    > Is that how it's usually done? Are there other/better ways?


    Thanks for all your suggestions.

    I figured I could make the non-member foo() function a static member of
    the Packet class. That way the member foo() can call it, and other code
    can refer to it as Packet::foo() if I understand correctly.

    If this solution works, I think I'm happy with it.

    Regards.
    Spoon, Dec 31, 2006
    #7
  8. Spoon

    Venkatesh Guest

    works fine..if i use scope it at global namespace..
    int foo() { return ::foo(x); }
    * got it compiled on vc8 & comaeu.
    -
    Venkatesh
    Spoon wrote:
    > Spoon wrote:
    >
    > > Consider the following code:
    > >
    > > int foo(int n) { return n+2; }
    > >
    > > class bar
    > > {
    > > public:
    > > int x;
    > > bar() { x = 42; }
    > > int foo() { return foo(x); }
    > > };
    > >
    > > int main()
    > > {
    > > bar baz;
    > > return baz.foo();
    > > }
    > >
    > > Apparently this doesn't work.
    > >
    > > test.cpp: In member function `int bar::foo()':
    > > test.cpp:8: error: no matching function for call to `bar::foo(int&)'
    > > test.cpp:8: note: candidates are: int bar::foo()
    > >
    > > I was told that the original foo lives in the unnamed namespace, and I
    > > that I could refer to that function with ::foo
    > >
    > > int foo(int n) { return n+2; }
    > >
    > > class bar
    > > {
    > > public:
    > > int x;
    > > bar() { x = 42; }
    > > int foo() { return ::foo(x); }
    > > };
    > >
    > > int main()
    > > {
    > > bar baz;
    > > return baz.foo();
    > > }
    > >
    > > Is that how it's usually done? Are there other/better ways?

    >
    > Thanks for all your suggestions.
    >
    > I figured I could make the non-member foo() function a static member of
    > the Packet class. That way the member foo() can call it, and other code
    > can refer to it as Packet::foo() if I understand correctly.
    >
    > If this solution works, I think I'm happy with it.
    >
    > Regards.
    Venkatesh, Jan 2, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page