By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,122 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,122 IT Pros & Developers. It's quick & easy.

Method definition calling a function with the same name

P: n/a
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.
Dec 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
Dec 29 '06 #2

P: n/a


On Dec 29, 3:24 pm, Spoon <devn...@localhost.comwrote:
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 '06 #3

P: n/a
"Spoon" <de*****@localhost.comwrote in message
news:45*********************@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.

Dec 29 '06 #4

P: n/a
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); }
};

Dec 29 '06 #5

P: n/a
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

Dec 30 '06 #6

P: n/a
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.
Dec 31 '06 #7

P: n/a
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.
Jan 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.