Click here to Skip to main content
15,890,282 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: use the member pointer via a Get function and allocate memory outside, not clear why in this way Pin
Andrew Brock1-Feb-11 2:17
Andrew Brock1-Feb-11 2:17 
Questionerror C2011: 'CMemDC' : 'class' type redefinition Pin
VCProgrammer31-Jan-11 21:25
VCProgrammer31-Jan-11 21:25 
AnswerRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
Cool_Dev31-Jan-11 21:38
Cool_Dev31-Jan-11 21:38 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
VCProgrammer31-Jan-11 21:41
VCProgrammer31-Jan-11 21:41 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
PJ Arends1-Feb-11 18:14
professionalPJ Arends1-Feb-11 18:14 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
Malli_S1-Feb-11 20:40
Malli_S1-Feb-11 20:40 
QuestionPreprocessor Directive Pin
pix_programmer31-Jan-11 17:34
pix_programmer31-Jan-11 17:34 
AnswerRe: Preprocessor Directive PinPopular
Andrew Brock31-Jan-11 17:48
Andrew Brock31-Jan-11 17:48 
It is very common that a header file will be included more than once in a single .cpp file.
If this happens then anything that is declared or defined in the header file will be defined more than once, even tho it is the same definition it will cause a compile error.
This is called a macro guard, and it ensures that the contents of the header file are only included once. an alternative to this is a single #pragma once at the top of the header file, however this only works with Visual Studio 7.0 and later.

Consider the folowing:
//Base.h
class Base {
};

//Sub1.h
#include "Base.h"
class Sub1 : public Base {
};

//Sub2.h
#include "Base.h"
class Sub2 : public Base {
};

//Main.cpp
#include "Sub1.h"
#include "Sub2.h"

int main() {
	Sub1 s1;
	Sub2 s2;
	//do stuff with the classes
	return 0;
}


Then the file Base.h will be included twice, hence the class Base will be defined twice.
This can happen with function definitions as well, so you should always use the macro guard.

Just note that at the end of the .h file there is also a #endif if you us the #ifndef

EDIT: The macro that is defined with #define and tested with #ifndef should be the same within the 1 header file, and unique amongst your project as well as any headers from other projects and libraries that you are using
GeneralRe: Preprocessor Directive Pin
Niklas L31-Jan-11 21:15
Niklas L31-Jan-11 21:15 
GeneralRe: Preprocessor Directive [modified] Pin
Stefan_Lang31-Jan-11 23:25
Stefan_Lang31-Jan-11 23:25 
GeneralRe: Preprocessor Directive Pin
Andrew Brock31-Jan-11 23:46
Andrew Brock31-Jan-11 23:46 
GeneralRe: Preprocessor Directive Pin
Stefan_Lang31-Jan-11 23:55
Stefan_Lang31-Jan-11 23:55 
GeneralRe: Preprocessor Directive Pin
Andrew Brock31-Jan-11 23:57
Andrew Brock31-Jan-11 23:57 
GeneralRe: Preprocessor Directive Pin
Niklas L1-Feb-11 1:50
Niklas L1-Feb-11 1:50 
GeneralRe: Preprocessor Directive Pin
Niklas L1-Feb-11 1:41
Niklas L1-Feb-11 1:41 
GeneralRe: Preprocessor Directive Pin
Stefan_Lang1-Feb-11 0:11
Stefan_Lang1-Feb-11 0:11 
AnswerRe: Preprocessor Directive Pin
Malli_S31-Jan-11 21:10
Malli_S31-Jan-11 21:10 
QuestionMFC - Printing - Changing page orientation from a custom pagesetup dialog [SOLVED] Pin
Un Suthee31-Jan-11 15:11
Un Suthee31-Jan-11 15:11 
AnswerRe: MFC - Printing - Changing page orientation from a custom pagesetup dialog Pin
Roger Allen2-Feb-11 5:46
Roger Allen2-Feb-11 5:46 
GeneralRe: MFC - Printing - Changing page orientation from a custom pagesetup dialog Pin
Un Suthee3-Feb-11 16:30
Un Suthee3-Feb-11 16:30 
Question2D dynamic wchar array Pin
csrss31-Jan-11 13:30
csrss31-Jan-11 13:30 
AnswerRe: 2D dynamic wchar array Pin
Andrew Brock31-Jan-11 16:41
Andrew Brock31-Jan-11 16:41 
GeneralRe: 2D dynamic wchar array Pin
csrss31-Jan-11 22:35
csrss31-Jan-11 22:35 
AnswerRe: 2D dynamic wchar array [DONE] Pin
csrss1-Feb-11 2:25
csrss1-Feb-11 2:25 
AnswerRe: 2D dynamic wchar array [modified] Pin
Emilio Garavaglia31-Jan-11 20:50
Emilio Garavaglia31-Jan-11 20:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.