Discussion:
How to use global function
(too old to reply)
b***@gmail.com
2007-08-04 05:12:41 UTC
Permalink
Hi
I think I am asking a silly question,but plz help me.I am defining a
function in global space and I want to use it in a class.but error is
coming saying that this function is undeclared identifier.How to do
that?
Giovanni Dicanio
2007-08-04 08:48:50 UTC
Permalink
Post by b***@gmail.com
I think I am asking a silly question,but plz help me.I am defining a
function in global space and I want to use it in a class.but error is
coming saying that this function is undeclared identifier.How to do
that?
Could you please post some code to diagnose?

Of course, it is possible to call a global function from a class, e.g. you
can develop a C++ class and call Win32 API functions (that are global) from
the class methods :)

Sometimes it may help to use the global scope specifier :: (e.g.
::MultiByteToWideChar) to specify that you want to access the global
function, but it is not mandatory (the :: helps when you want to
disambiguate when you have a global function and a "local" function/method
having the same name).

Giovanni
David Wilkinson
2007-08-04 10:00:41 UTC
Permalink
Post by b***@gmail.com
Hi
I think I am asking a silly question,but plz help me.I am defining a
function in global space and I want to use it in a class.but error is
coming saying that this function is undeclared identifier.How to do
that?
bhattacharjeesoft:

Do you have a header file containing the prototype for this function? If
so, you must #include it in the file where you are trying to use the
function.

Remember how C/C++ compilation works. Each implementation file (.cpp or
.c file) is compiled separately. Anything the compiler needs to know
must be #include'd using header files.
--
David Wilkinson
Visual C++ MVP
Tom Serface
2007-08-06 15:15:20 UTC
Permalink
Hi,

To add to what David wrote. I'm not a big fan of global functions (or
variables), but for the ones I do use I put them in a file (called
GlobalFunctions.cpp) and there is a corresponding header file (called
GlobalFunctions.h) that gets included anywhere I need to use any of the
functions. That makes it easy to add them and easy to know where they are
used. It's kind of a crude attempt at OOP programming without an object :o)

Tom
Post by b***@gmail.com
Hi
I think I am asking a silly question,but plz help me.I am defining a
function in global space and I want to use it in a class.but error is
coming saying that this function is undeclared identifier.How to do
that?
Klic
2007-08-08 07:46:16 UTC
Permalink
Hi ***@gmail.com & All ,

I think it is very easy to use globale function.

Suppose the class Test.cpp

1. Wite a defination of this function.
2. Write declaration of this function in top level of class(means
below the #include).
3. suppose you want to use this function in another class just declare
this function in top level of this class using "extern".
4. Now freely call and use the function.


Actually your problem is you written the function defination but not
wrote the declaration. So it is giving you compilation error as it is
not getting declaration.


Cheers,
-klic

Loading...