Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Method definition calling a function with the same name

Reply
Thread Tools

Method definition calling a function with the same name

 
 
Spoon
Guest
Posts: n/a
 
      12-29-2006
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.
 
Reply With Quote
 
 
 
 
Sandip Shahane
Guest
Posts: n/a
 
      12-29-2006
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.


 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      12-29-2006


On Dec 29, 3:24 pm, Spoon <devn...@localhost.com> 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;
}

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      12-29-2006
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); }
};

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-29-2006
"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.



 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      12-30-2006
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

 
Reply With Quote
 
Spoon
Guest
Posts: n/a
 
      12-31-2006
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.
 
Reply With Quote
 
Venkatesh
Guest
Posts: n/a
 
      01-02-2007
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a method with 'throws IOException' from another method in the same class rleroux@telus.net Java 1 01-28-2007 06:30 AM
Find function definition in a C file for a function with its name and starting line no. as input jeniffer Perl Misc 7 03-26-2006 10:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Calling a global function with the same name as a member function anoopsaha@gmail.com C++ 5 01-05-2005 09:40 AM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments