473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Dec 29 '06 #1
7 1932
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


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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
1
by: Vijay | last post by:
Hi, I do have the VB.NET method which has delegate as parameter. I am calling this method from VB.NET project by using the commnad "Addressof( Function name which has same signature)". How can i...
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
4
by: Panos Laganakos | last post by:
I want a class method to take action depending on the type of the arguement passed to it. ie: getBook(id) # get the book by ID getBook(name) # get the book by name .... Other languages use...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
9
by: 7stud | last post by:
Hi, I'm trying to figure out what this passage from GvR's tutorial means: ------- Class definitions, like function definitions (def statements) must be executed before they have any effect.......
1
by: wang frank | last post by:
Hi, I am trying to write a python class with a new data type such as: class Cc14: def __init__(self, realpart, imagpart): self.r=realart self.i=imagpart def __saturator(x): return x+1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.