Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to a generic class

Status
Not open for further replies.

nicktherod

Programmer
Nov 10, 2000
19
CA
I have created a class CFileUpdate using CObject as the base. I now want a pointer to that class to call specific functions. I have made sure that i have put in all the #include's that are needed and then added the variable in another class:

CFileUpdate* m_pFileUpdate;

but i keep getting a syntax error such as:

error C2143: syntax error : missing ';' before '*'

on that line.

Could some kind soul please help me.

Many Thanks
 
Let's suppose you declare the CFileUpdate class in FileUpdate.h file and implement it into FileUpdate.cpp file.

Let's also suppose you want to declare a pointer to a CFileUpdate object into another class, CMyClass. Its header file MyClass.h should contains only a forward declaration of CFileUpdate class:
Code:
//file MyClass.h
#ifndef __MY_CLASS_ //don't forget this in any .H file!
#define __MY_CLASS_

class CFileUpdate;  //forward declaration

class CMyClass {

 CFileUpdate* m_pFileUpdate;

public:
  SetFile(CFileUpdate* pFileUpdate)
  {
    m_pFileUpdate = pFileUpdate;
  }

};
#endif // __MY_CLASS_
You should include the header FileUpdate.h only in implementation file of CMyClass (MyClass.cpp):
Code:
//file MyClass.cpp
#include <stdafx.h>
#include &quot;FileUpdate.h&quot;

...

Avoid #include in .H files.
The forward declaration is enough as long as you don't use that pointer to call its member functions or access its public data, or try to new/delete it.

Marius Samoila
Brainbench MVP for Visual C++
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top