![]() |
Method definition calling a function with the same name
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. |
Re: Method definition calling a function with the same name
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. |
Re: Method definition calling a function with the same name
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; } |
Re: Method definition calling a function with the same name
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); } }; |
Re: Method definition calling a function with the same name
"Spoon" <devnull@localhost.com> wrote in message
news:4595253f$0$321$426a74cc@news.free.fr... > 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. |
Re: Method definition calling a function with the same name
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 |
Re: Method definition calling a function with the same name
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. |
Re: Method definition calling a function with the same name
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. |
| All times are GMT. The time now is 09:45 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.