negative_entropy
asked on
Stupid newbie question re: #include
Hi Guys,
Got a real dumb question for you, but it is someting I for the life of me cannot work out...
Where do I put the #include directives for any new classes that I add? I have been putting them in StdAfx.h as it seems to want me to, but for some reason, the classes I have been creating with the class wizard are not being recognised in other parts of the project. e.g:
// StdAfx.h
#include "firstDlg.h"
#include "secondDlg.h"
#include "NewClass.h"
// Application.h
class CApplicationApp : public CWinApp
{
// other initialisation...
CNewClass* m_pNewClass;
// the above line fails
}
Does anyone have any ideas?
Got a real dumb question for you, but it is someting I for the life of me cannot work out...
Where do I put the #include directives for any new classes that I add? I have been putting them in StdAfx.h as it seems to want me to, but for some reason, the classes I have been creating with the class wizard are not being recognised in other parts of the project. e.g:
// StdAfx.h
#include "firstDlg.h"
#include "secondDlg.h"
#include "NewClass.h"
// Application.h
class CApplicationApp : public CWinApp
{
// other initialisation...
CNewClass* m_pNewClass;
// the above line fails
}
Does anyone have any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
make a correction
instead of >> class NewClass;
class CNewClass; // the classname which u want to access
instead of >> class NewClass;
class CNewClass; // the classname which u want to access
BTW, the second method 1cand01t suggested is called 'forwarding' and may save
a lot of compile time in larger projects because you don't have to include "NewClass.h"
into any other files which don't use CNewClass but need to include "Application.h"
a lot of compile time in larger projects because you don't have to include "NewClass.h"
into any other files which don't use CNewClass but need to include "Application.h"
2 negative_entropy
In most cases you must NOT insert your include directive in "StdAfx.h". Try to follow next rules for minimize build time.
1. If you need to use new class in member function(s) insert include directive in *.cpp
2. If you need to use new class in class declaration (*.h) as pointer use forward class declaration in *.h (class CMyClass;) and include directive in *.cpp
3. Otherwise use construction like this:
#ifndef _USEFULL_H_
#define _USEFULL_H_
#include "MyClass.h"
...
#endif //_USEFULL_H_
4. If you use external library or use declaration which not changed oft AND used in the whole project then place include directive in "StdAfx.h".
In most cases you must NOT insert your include directive in "StdAfx.h". Try to follow next rules for minimize build time.
1. If you need to use new class in member function(s) insert include directive in *.cpp
2. If you need to use new class in class declaration (*.h) as pointer use forward class declaration in *.h (class CMyClass;) and include directive in *.cpp
3. Otherwise use construction like this:
#ifndef _USEFULL_H_
#define _USEFULL_H_
#include "MyClass.h"
...
#endif //_USEFULL_H_
4. If you use external library or use declaration which not changed oft AND used in the whole project then place include directive in "StdAfx.h".
StdAfx.h file**********************
#include "class1.h"
**************************
file1.h*******************
#include "StdAfx.h"
class1 can be accessed.
**************************
file2.h*******************
(StdAfx.h not included)
(class1.h not included)
cannot access class1.
**************************
file3.h*******************
#include "class1.h"
class1 can be accessed.
**************************
hope that clears things up...