![]() |
Function pointer?
Is it possible to define function pointer globally?
I'm getting strange errors when I do that, but defining it in function body works fine. :
Bye |
Well if you read the error message properly and try to do some of your own thinking it will help you.
What do you think the first line, multiple definition of `show' ", and the last line, "first defined here" means? When you have globals (which are a bad idea) you run the risk of have to Identifiers with the same name. This if probably what is happening here. The name show is used somewhere in the the depths of the C++ library you are using. To solve it you will have to change your identifier name to something else or not have it as a global. |
This was the case of multiple include problem it seems...
Here is the problem: Function pointer is in header file which gets included by multiple source files and then in linking process I get "multiple definition" error. How do I put the definition of function pointer in header file without geting the linker error? Bye |
You need to understand the distinction between a definition and a declaration. The following may seem a bit long, but it'll walk you through the things you need to know to fix your problem.
I'll use a global variable of type int to illustrate, but the idea's exactly the same if the global is of a different type (eg a pointer to a function). Within a complete program (i.e. what is assembled by the linker, if it succeeds) there can only be one instance of a global. The way you ensure there is an instance is to define it. In a single source file, this is easy; :
int global_var;But what happens if we break the project up into parts. First a header file declaring our global; :
// header file named global.h:
// global.cpp (assume cpp is an extension for C++ files recognised by our compiler):
// main.cpp (assume cpp is an extension for C++ files recognised by our compiler)The above will, typically, result in a multiple definition error such you have seen. The reason is that, when compiling each source file, the compiler sees the declaration; :
int global_var;This means we have to help the linker out so there is only one definition globally. That means we need to tell the compiler to do things differently. The formal description (in the C++ standard) is that we need to obey the "one definition rule" -- if anything is defined in more than one way within a complete program, we have undefined behaviour. In the above example, we have defined global_var twice, which intuitively might suggest we have violated something called the "one definition rule". To fix the problem, we come back to the notion that a definition is a particular type of declaration. So we need to introduce a declaration that does not make the compiler generate one definition for each source file. The header file is the thing used by both source files, so let's have a look there. :
// header file named global.h:
// header file named global.hSo we have to introduce one, and only one definition somewhere so the linker sees a definition. If we put such a definition into a header file, we're back where we started. So, we have to put it into one (and only one) source file. It doesn't matter where we put it, as long as it is in a source file that has #include'd our global.h. I'll put it into main.cpp :
// main.cpp (assume cpp is an extension for C++ files recognised by our compiler)The only other comment is that the header file has what is called an include guard. It is a technique designed to prevent confusing the compiler should a header file be #included more than once. The name of the macro GLOBAL_H_INCLUDED needs to be chosen unique and distinct for every header file. |
My reply is a bit shorter.
Thanks! |
| All times are GMT -5. The time now is 2:01 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC