Thread: Re: [Dev-C++] Multiply-included header problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: FerretallicA <fer...@ho...> - 2003-07-27 06:15:04
|
Oo oi, OK got it working... I tried the following code and it made no difference, but I tried doing a clean build and now it's worked. The solution is basic, but here it is anyway for those who might run into this problem at some point. MAIN.H #ifndef PRAGMAONCE #define PRAGMAONCE int some_important_variable; #endif PRAGMAONCE can be renamed to whatever you feel like if you really want. -FerretallicA ----- Original Message ----- From: "FerretallicA" <fer...@ho...> To: "Dev-C++ Users Mailing List" <dev...@li...> Sent: Sunday, July 27, 2003 4:03 PM Subject: [Dev-C++] Multiply-included header problem Oo oi, I have a bit of a dilemma, albiet a seemingly simplistic one... Say I have 3 files as follows- MAIN.CPP #include "main.h" EXTRAFUNCTIONS.CPP #include "main.h" MAIN.H int some_variable_important_to_lots_of_functions; I keep getting errors that some_variable_important_to_lots_of_functions is being redefined. I have tried the "#pragma once" solution which worked with Visual C++ but it doesn't seem to work in Dev-C++. I tried the following- MAIN.CPP #include "main.h" int some_variable_important_to_lots_of_functions; EXTRAFUNCTIONS.CPP #include "main.h" MAIN.H extern int some_variable_important_to_lots_of_functions; Still the same problem. Does anyone know of a solution to this? Is this some mistake I am making, a problem with Dev-C++'s compiler, or what? -FerretallicA ------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: <or...@vp...> - 2003-07-27 08:34:58
|
FerretallicA wrote: > Oo oi, > > OK got it working... I tried the following code and it made no difference, > but I tried doing a clean build and now it's worked. The solution is basic, > but here it is anyway for those who might run into this problem at some > point. > > MAIN.H > #ifndef PRAGMAONCE > #define PRAGMAONCE > int some_important_variable; > #endif > > > PRAGMAONCE can be renamed to whatever you feel like if you really want. It actually SHOULD be renamed, and it should also be unique in each header file. Because if not, some header files will not be included. So I suggest, you name it something like this: #ifndef _MAIN_H_ #define _MAIN_H_ int some_important_variable; #endif -- Greetings, Balázs |
|
From: Per W. <pw...@ia...> - 2003-07-27 15:04:11
|
But that's not what FerretallicA wanted. He tried to implement main.h: #ifndef _MAIN_H_ ... #if INSTANTIATE_GLOBALS #define GLOBAL #else #define GLOBAL extern #endif GLOBAL int some_important_variable; #endif _MAIN_H_ main.cpp: #define INSTANTIATE_GLOBALS #include "main.h" // Instantiate needed global variables. extrafunctions.cpp: #include "main.h" // Just thell what global variables are available. Renaming PRAGMAONCE to _MAIN_H would still result in all source files including main.h getting a copy of the global variable. /Per W On Sun, 27 Jul 2003, OROSZI Bal=E1zs wrote: > FerretallicA wrote: > > Oo oi, > > > > OK got it working... I tried the following code and it made no differen= ce, > > but I tried doing a clean build and now it's worked. The solution is ba= sic, > > but here it is anyway for those who might run into this problem at some > > point. > > > > MAIN.H > > #ifndef PRAGMAONCE > > #define PRAGMAONCE > > int some_important_variable; > > #endif > > > > > > PRAGMAONCE can be renamed to whatever you feel like if you really want. > > It actually SHOULD be renamed, and it should also be unique in each > header file. Because if not, some header files will not be included. > So I suggest, you name it something like this: > > #ifndef _MAIN_H_ > #define _MAIN_H_ > int some_important_variable; > #endif > > -- > Greetings, > Bal=E1zs > > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/= 01 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: <or...@vp...> - 2003-07-27 17:53:19
|
Per Westermark wrote: > But that's not what FerretallicA wanted. I didn't care about the content - I haven't even read that part. Remember my last misunderstanding? Regarding "FindFirstFile", etc.? :) Something like this happened here again. I only read that #define stuff, and not the content, and I haven't thought of the actual goal of his program. Maybe I should really read these mails thoroughly... BTW, you are right about this. The variable HAS to be placed in the .cpp, and only the definition of it into the header (extern xxx). So to have only one variable, and not multiple copies. -- Greetings, Balázs |
|
From: P.D. v. Z. <pa...@my...> - 2003-07-27 11:58:21
|
Hi, I'm trying to implement a pointer-to-member callback function in one of my classes, and I finally seem to have the syntax right. I have a member function whose prototype looks like this: template<typename T> void Parse(const std::string& strFile, T* pClass, bool (T::* pCB)(const ConfigLine&)) It's a member function of a non-template class ConfigParser. When I try to use this function however, I get a linker error. "[Linker error] undefined reference to `void ConfigParser::Parse<Config>(std::string const&, Config*, bool (Config::*)(ConfigLine const&))'" Now I've browsed through numerous tutorials, checked my "The C++ Programming Language" book, and did a bunch of Google searches, but I still can't find what I'm doing wrong. I could send the relevant code along, but won't bother you with it just yet. I would guess that if I define and implement the template function, I shouldn't be getting any linker errors for whatever type I put into it. I hope anyone can help me, cause I'm starting to consider making the entire class a template, just to get rid of the linker error. I already switched from Visual C++ 6 to GCC because of one of the many bugs in VC++6, one of which is that it doesn't seem to understand/support template member functions. Thanks in advance, Paul |
|
From: Tom C. <jat...@nt...> - 2003-07-27 17:26:11
|
"P.D. van Zelst" <pa...@my...> writes: > I'm trying to implement a pointer-to-member callback function in one of > my classes, and I finally seem to have the syntax right. I have a member > function whose prototype looks like this: > > template<typename T> void Parse(const std::string& strFile, T* pClass, > bool (T::* pCB)(const ConfigLine&)) > > It's a member function of a non-template class ConfigParser. When I try > to use this function however, I get a linker error. "[Linker error] > undefined reference to `void ConfigParser::Parse<Config>(std::string > const&, Config*, bool (Config::*)(ConfigLine const&))'" Try placing the definition of ConfigParser::Parse in a separate header file, which you should then #include at the bottom of the header file containing the class definition of ConfigParser. It means more must be recompiled when either header file is changed, but it's the only solution I've found to this most annoying of problems. I understand the C++ 'export' keyword is designed to alleviate the problem, but neither GCC nor Visual C++ currently supports it. --Tom |
|
From: Per W. <pw...@ia...> - 2003-07-27 14:58:14
|
Your second solution, the one with // main.h extern int some_important_variable; // main.cpp int some_important_variable; should work too - it's the normal way of accessing a variable between multiple modules. I assume that all you needed to to was to perform a full rebuild, since the extrafunctions.cpp already contained it's own copy of the variable. /Per W On Sun, 27 Jul 2003, FerretallicA wrote: > Oo oi, > > OK got it working... I tried the following code and it made no difference, > but I tried doing a clean build and now it's worked. The solution is basic, > but here it is anyway for those who might run into this problem at some > point. > > MAIN.H > #ifndef PRAGMAONCE > #define PRAGMAONCE > int some_important_variable; > #endif > > > PRAGMAONCE can be renamed to whatever you feel like if you really want. > > -FerretallicA > > ----- Original Message ----- > From: "FerretallicA" <fer...@ho...> > To: "Dev-C++ Users Mailing List" <dev...@li...> > Sent: Sunday, July 27, 2003 4:03 PM > Subject: [Dev-C++] Multiply-included header problem > > > Oo oi, > > I have a bit of a dilemma, albiet a seemingly simplistic one... > Say I have 3 files as follows- > > > MAIN.CPP > #include "main.h" > > EXTRAFUNCTIONS.CPP > #include "main.h" > > MAIN.H > int some_variable_important_to_lots_of_functions; > > > I keep getting errors that some_variable_important_to_lots_of_functions is > being redefined. I have tried the "#pragma once" solution which worked with > Visual C++ but it doesn't seem to work in Dev-C++. I tried > the following- > > > MAIN.CPP > #include "main.h" > int some_variable_important_to_lots_of_functions; > > EXTRAFUNCTIONS.CPP > #include "main.h" > > MAIN.H > extern int some_variable_important_to_lots_of_functions; > > > Still the same problem. Does anyone know of a solution to this? Is this some > mistake I am making, a problem with Dev-C++'s compiler, or what? > > -FerretallicA > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |