Archived

This topic is now archived and is closed to further replies.

Lanman

class problem...

Recommended Posts

Lanman    122
Well, I have 3 files file1.cpp file2.cpp and shared header head.hpp I want to create a class class CTest { public: void Hello(void); int Value; }; in file head.hpp and access this class from any file...how to create it ? I allways get error. ERROR: class CTest is allready defined in module blahblahblah or when i use extern ERROR: unresolved external any ideas ? thax !

Share this post


Link to post
Share on other sites
Xai    1848
All of you header files need to use preprocessor directives to prevent this problem ... here''s how:

at the top of your header put

  
#ifndef MY_FILE_H
#define MY_FILE_H


and at the bottom of your file put

  
#endif


and that shuold do it ...

note that you need to replace MY_FILE_H with a unique name for each file ... my convention is this ... if my file names are:

SharedSerialManager.h and SharedSerialAgent.h

my defines use the names

SHARED_SERIAL_MANAGER_H and SHARED_SERIAL_AGENT_H

which is easy to read ... and is nearly guaranteed not to overlap any other defines.

good luck.

Share this post


Link to post
Share on other sites
Lanman    122
Thanx for the reply, but I still have the problem...

I''m trying to do everything! 2 files, shared header with code:

#ifndef __FILE_H__
#define __FILE_H__

class CTest {
public:
void XXX(void);
int DDD;
};

#endif

and in file 1 is:

#include <windows.h>
#include "FILE.H"
CTest *Test;
int WINAPI WinMain(HINSTANCE hI, HINSTANCE pI, LPSTR Cmd, int S)
{
Test->DDD = 10;
Test->XXX();
return 0;
}

and in file 2 is:

#include <windows.h>
#include "FILE.H"
CTest *Test;

void CTest::XXX(void)
{
if (Test->DDD==10) MessageBox(NULL,"OK","OK",MB_OK);
return;
}

THIS SHOULD WORK RIGHT...but an error occures:
ERROR: class _CTest was already defined in module ...
or when I try to use extern:
ERROR: unresolved external class _CTest ...

i don''t know what to do ! I''ve tried crazy things and I''ve made this work one time, but I sayed "Program has rised an illegal instriction" (or such - I don''t know - i don''t have english windows but you certainly know what i mean ... )
PLEASE HELP ! THANKS !!!

Share this post


Link to post
Share on other sites
Steveyboy    122
File.h:
    
// #define etc.


class CTest
{
};

extern CTest* Test;

In File.cpp:
        
#include "File.h"

CTest* Test;

// ...


In Main.cpp:
  
#include "File.h"

void main()
{
Test.XXX();
}

You only want "extern" in the header file, and you only want to declare the CTest object once - in the source file (.cpp) that belongs to the header.

HTH, Steve

Edited by - Steveyboy on February 8, 2002 3:35:46 PM

Share this post


Link to post
Share on other sites