Solved

Stupid newbie question re: #include

Posted on 2002-04-24
5
147 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:negative_entropy
5 Comments
 

Expert Comment

by:Jedimaster
ID: 6967590
by putting the #include in StdAfx.h you are basically giving all of you classes access to those included classes.  if you look, MFC automatically includes StdAfx.h in every .cpp file you create.  so... if you include the class in StdAfx.h and the class you are working in has StdAfx.h included in it then the class you are working in can see the other class.  Confused yet? ;0)  lets try this...


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...
0
 

Accepted Solution

by:
1cand01t earned 50 total points
ID: 6967744
in Application.h
try one of the following line
either
#include "NewClass.h"

or
class NewClass;
=======================================

// Application.h

//************-either-****************

#include "NewClass.h"

//**************-or-******************

class NewClass;

//************************************

class CApplicationApp : public CWinApp
{
    // other initialisation...

    CNewClass* m_pNewClass;
    // the above line will not fail
}

good luck
0
 

Expert Comment

by:1cand01t
ID: 6967751
make a correction
instead of >> class NewClass;

class CNewClass; // the classname which u want to access
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6968205
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"
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6968248
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".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 111
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
ColdFusion Rereplace 3 71
x-bar in Google Sheets 2 26
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now