Method definition calling a function with the same name

S

Spoon

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.
 
S

Sandip Shahane

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.
 
W

wittempj

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;
}
 
G

Gianni Mariani

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); }
};
 
J

Jim Langston

Spoon said:
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.
 
I

Ivan Novick

Spoon said:
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.
 
S

Spoon

Spoon said:
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.
 
V

Venkatesh

works fine..if i use scope it at global namespace..
int foo() { return ::foo(x); }
* got it compiled on vc8 & comaeu.
-
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top