Sign in to follow this  
EvilNando

Linking Problems

Recommended Posts

EvilNando    96
hello im having some troubles linking my classes together what im doing is this System.h -> all globals defines and includes are here Application.h -> links to System.h CDirect3D.h -> links to System.h i must say also that Application has a member of type Cdirect3D that is instatiated at the constructor and CDirect3d has a member *WindowApp pointing to a Application class object that is also generated at the constructor i cant compile this thing and i dont know what im doing wrong any help? [edit] moved to the begginers forum [/edit]

Share this post


Link to post
Share on other sites
EvilNando    96
no im not using none of those

heres some code

//**************************************************/
// CApplication.h
// Class responsible for bulding the window framework
// for the DirectX Game
// Armando Alva@2004
//**************************************************/

//**************************************************/
#pragma once

// Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN

// Link in the required libraries
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "winmm.lib")

#include <d3dx9.h>
#include <windows.h>
#include "CDirect3D.h"
//**************************************************/

class CApplication
{
public:

/* Constructor */
CApplication(void);

/* Destructor */
virtual ~CApplication(void);

/* Message Procedures */
virtual LRESULT WINAPI MessageProcedure(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

/* Start all CApplication systems */
void Run();

protected:

/* This Function Creates the Window */
bool MakeWindow(int iWidth, int iHeight, bool bWindowed);

/* Main Loop Function */
void GameLoop(void);

virtual bool FirstInitialize() { return TRUE; } // Called at the beginning
virtual bool FinalCleanup() { return TRUE; } // Called at the end
virtual int PreRender(); // Called before rendering
virtual int Render(); // Called after rendering


HWND m_hWnd; // Handle of the Window CApplication
bool m_bWindowed; // Is the CApplication windowed or fullscreen
UINT m_iWndWidth; // Windowed width
UINT m_iWndHeight; // Windowed height
bool m_bActive; // Flag to check if window is active
bool m_bPaused; // Flag to check if window is paused
bool m_bAlive; // Flag to check if window is alive

char m_strTitle[128]; // Our CApplications title
char m_strClass[128]; // Our CApplications unique class name


// DIRECT3D ENGINE CLASS //
CDirect3D* CD3DEngine;
// DIRECT3D ENGINE CLASS //


};




//**************************************************/
// CApplication.cpp
// Declarations for Applciation.h
//
// Armando Alva@2004
//**************************************************/

#include "Application.h"

// This header is so we can assign this message handler to the WNDCLASSEX structure
LRESULT WINAPI MainMessageProcedure(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

CApplication::CApplication(void)
{
// Initiliaze all Class Members //

m_bWindowed = true;
m_hWnd = NULL;
m_iWndWidth = 800;
m_iWndHeight = 600;
m_bActive = true;
m_bAlive = true;
m_bPaused = false;
CD3DEngine = NULL;

strcpy(m_strClass, "WindowsClass");
strcpy(m_strTitle, "Game Program");

// Create the 3D Engine Class //
CD3DEngine = new CDirect3D;

}

// PreRender Function //
int CApplication::PreRender(void)
{
if(!CD3DEngine->PreRender3D())
{
return 0;
}

return 1;
}


// Render Function //
int CApplication::Render(void)
{
if(!CD3DEngine->Render3D())
{
return 0;
}

return 1;

}




//**************************************************/
// 3DEngine.h
// Class responsible for all the D3D initialization
// for the DirectX Game
// Armando Alva@2004
//**************************************************/

#include "Application.h"


class CDirect3D
{
public:

/* Constructor & Destructor */
CDirect3D(CApplication& AppClass);
~CDirect3D(void);


/* Start the whole 3D process */
int Initialize3D(void);

/* End the whole 3D process */
void Terminate3D(void);

/* Prerender Settings */
int PreRender3D(void);

/* Render Settings */
int Render3D(void);

private:

HRESULT m_hResult; /* Here its stored any error msgs */

IDirect3D9* m_pD3DObject; /* DirectX9 Object */
IDirect3DDevice9* m_pD3DDevice; /* DirectX9 Device */
D3DFORMAT m_D3DFormat; /* DirectX9 Format */
D3DPRESENT_PARAMETERS m_D3DPP; /* DirectX9 Present Parameters */

CApplication* m_pAppClass; /* Pointer to the window class */

};



//**************************************************/
// 3DEngine.cpp
// Declarations for 3DEngine.h
//
// Armando Alva@2004
//**************************************************/

#include "Application.h"
#include "CDirect3D.h"

/* Constructor */
//**************************************************/

CDirect3D::CDirect3D(CApplication& AppClass)
{
// Initialize Members

m_D3DFormat = D3DFMT_R5G6B5;
m_pD3DDevice = NULL;
m_pD3DObject = NULL;
m_pAppClass = AppClass;

ZeroMemory(&m_D3DPP, sizeof(D3DPRESENT_PARAMETERS));

// Set the address of the main application handle here

}

/* Initialize3D */
//**************************************************/

int CDirect3D::Initialize3D(void)
{
m_pD3DObject = Direct3DCreate9( D3D_SDK_VERSION );

if(!m_pD3DObject)
{
MessageBox(m_pAppClass->m_hWnd, "Direct3DCreate9 Failed!", "Error", MB_OK);
return 0;
}

// D3DPRESENT PARAMETERS INIT

m_D3DPP.BackBufferCount = 1; // A single back buffer is needed here
m_D3DPP.MultiSampleType = D3DMULTISAMPLE_NONE; // No multisampling
m_D3DPP.MultiSampleQuality = 0; // No multisampling
m_D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;// Throw away processed frames they are not needed
m_D3DPP.hDeviceWindow = m_pAppClass->m_hWnd; // Window handle
m_D3DPP.Flags = 0; // No Flags to set
m_D3DPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //Default Refresh Rate
m_D3DPP.PresentationInterval= D3DPRESENT_INTERVAL_DEFAULT; //Default Presentation rate
m_D3DPP.BackBufferFormat = m_D3DFormat; // Bytes used for the display
m_D3DPP.EnableAutoDepthStencil = FALSE; // No depth stencil buffer

if(m_pAppClass->m_bWindowed)
{
m_D3DPP.Windowed = true; // Set windowed mode ON
}
else
{
m_D3DPP.Windowed = false;
m_D3DPP.BackBufferWidth = 800;
m_D3DPP.BackBufferHeight= 600;
}


// Now create the device

m_hResult = m_pD3DObject->CreateDevice(D3DADAPTER_DEFAULT, // The default adapter
D3DDEVTYPE_HAL, // Use Hardware Acceleration
m_AppClass->m_hWnd, // Handle to the application
D3DCREATE_SOFTWARE_VERTEXPROCESSING,// process vertex in software for compatibility
&m_D3DPP, // The app PRESENT_PARAMETERS
&m_pD3DDevice); // Now pointing to the new created device

if(FAILED(m_hResult))
{
MessageBox(m_AppClass->m_hWnd, "CreateDevice Failed!", "Error", MB_OK);
return 0;
}

return 1;

}

/* Terminate 3D */
//**************************************************/

void CDirect3D::Terminate3D(void)
{
// check for a valid D3D device
if(m_pD3DObject)
{
m_pD3DObject->Release();

// NULL the pointer just to be safe
m_pD3DObject = NULL;
}
}



the errors im getting ...


CDirect3D.h(41): error C2501: 'CDirect3D::Application' : missing storage-class or type specifiers




CDirect3D.h(41): error C2501: 'CDirect3D::CApplication' : missing storage-class or type specifiers
CDirect3D.h(41): error C2501: 'CDirect3D::m_pAppClass' : missing storage-class or type specifiers
CDirect3D.h(12): error C2011: 'CDirect3D' : 'class' type redefinition
CDirect3D.h(12) : see declaration of 'CDirect3D'
CDirect3D.cpp(15): error C2511: 'CDirect3D::CDirect3D(CApplication &)' : overloaded member function not found in 'CDirect3D'

amongs others

thanks !

Share this post


Link to post
Share on other sites
pjcast    311
This is what I see for starters:

You have an include problem. You see, in C/C++, the preprocessor just inserts anything (dumps) that is included into the compilation unit. Also, C needs to know what a structure is before it is first used....

SO heres what your problem seems to be:

First, CDirect3D.cpp (according to the compiler message, though you have it called 3DEngine.cpp in the source listing) is what is currently compiling (compilation unit). THe first thing it sees is #include "application.h" ... SO it dumps that into current CU (compilation unit) and starts parsin it... So now, we are processing the pragma's in that header, which is fine... But the mess comes when the #include CDirect.h is found.

That now gets dumped into the CU. Ok... SO we now process that part. Now, the first thing that is called from CDirect.h is #include Application ****** Here is the Problem ******. Since this header file was already (semi processed anyway) and was told to pragma once... CApplication has yet to be defined!

So of course you will get errors that mean that CApplication is not a type.

Share this post


Link to post
Share on other sites
EvilNando    96
Quote:
Original post by pjcast
This is what I see for starters:

You have an include problem. You see, in C/C++, the preprocessor just inserts anything (dumps) that is included into the compilation unit. Also, C needs to know what a structure is before it is first used....

SO heres what your problem seems to be:

First, CDirect3D.cpp (according to the compiler message, though you have it called 3DEngine.cpp in the source listing) is what is currently compiling (compilation unit). THe first thing it sees is #include "application.h" ... SO it dumps that into current CU (compilation unit) and starts parsin it... So now, we are processing the pragma's in that header, which is fine... But the mess comes when the #include CDirect.h is found.

That now gets dumped into the CU. Ok... SO we now process that part. Now, the first thing that is called from CDirect.h is #include Application ****** Here is the Problem ******. Since this header file was already (semi processed anyway) and was told to pragma once... CApplication has yet to be defined!

So of course you will get errors that mean that CApplication is not a type.


So what do you think I can do to solve that?

Share this post


Link to post
Share on other sites
pjcast    311
Check out the link I posted.

But, for a quick rundown:

Be careful having your own include files including other include files. For instance:

If class A needs to know about class B, and class B needs to know about class A, forward declare one of the classes instead of including the header file.

So, in your case:

In appliation.h, remove the include CDirect3d.h line . Replace it with:

class CDirect3D;

And in CDirect3D.h, remove the #include Application.h, and add this some where above your class definition:

class CApplication;

Now, because you only use pointers in your header files, this will work perfectly for you. But, in both your source files, you will need to include the CDirect.h & CApplication.h headers.

Share this post


Link to post
Share on other sites
EvilNando    96
thank you so much
i now figured what to do
but now ive stumbled into another trouble

how u solve this

example:

for my game im using 2 classes right now

1. Is CApplication, in charge of registering the window and creating it

2. Is CDirect3D, in charge of creating the display device and locking the surface

now

what ive been trying to the whole day is this

put a member in CApplication of type CDirect3D so when the window is created i can make a call to the 3drendering stuff

but since all D3D stuff needs a handle to a window on the CDirect3D class I need a pointer to a CApplication class

well what ive been trying to do is create a Direct3D class at the constructor of the CApplication

and make the constructor of the CDirect3D class to take an address of CApplication as a parameter

but its not working

can u help me?

Share this post


Link to post
Share on other sites
pjcast    311
I'm all about helping ;)

Ok, you say what you are trying to do isn't working. So I take it you can compile and link, but you just don't know how to call your overloaded constructor ( CDirect3D::CDirect3D(CApplication &pApp) )that takes an Application reference?

Where you can do that, is in your constructor. You have this right now:
CD3DEngine = new CDirect3D;

But you should make it:
CD3DEngine = new CDirect3D( this );

That will call the constructor taking a CApplication class.
Using the "this" pointer.

And just for your reference, the compiler may throw a warning at you about using the this pointer before CApplication is fully constructed; however, since you are only saving it and not doing anything dangerous with it, it is safe to ignore.


CApplication::CApplication(void)
{
// Initiliaze all Class Members //

m_bWindowed = true;
m_hWnd = NULL;
m_iWndWidth = 800;
m_iWndHeight = 600;
m_bActive = true;
m_bAlive = true;
m_bPaused = false;
CD3DEngine = NULL;

strcpy(m_strClass, "WindowsClass");
strcpy(m_strTitle, "Game Program");

// Create the 3D Engine Class //
CD3DEngine = new CDirect3D(this);

}

Share this post


Link to post
Share on other sites
EvilNando    96
somehow i knew i had to used this pointer but was afraid to do it
thanks a lot
ill post my results as soon as i can

thanks! :)

Im getting a class redefinition problem
can u check my headers


//**************************************************/
// 3DEngine.h
// Class responsible for all the D3D initialization
// for the DirectX Game
// Armando Alva@2004
//**************************************************/

#include "System.h"
// #include "CApplication.h"




//**************************************************/
// CApplication.h
// Class responsible for bulding the window framework
// for the DirectX Game
// Armando Alva@2004
//**************************************************/

#include "System.h"

#include "CDirect3D.h"


the error is
CDirect3D.h(15): error C2011: 'CDirect3D' : 'class' type redefinition




[Edited by - EvilNando on July 8, 2004 8:56:49 PM]

Share this post


Link to post
Share on other sites
pjcast    311
Well, usually that happens when your headers include other headers. Pretty much the same error you were having before. In application.h .Remove the #Include CDirect3d.h and instead use class CDirect3D

Also, make sure that in system.h you are not including in CDirect3D.h. You should thourougly read that link I posted earlier if you havn't already, but if ou have and are still having problems then I am all for helping you.

Another thing you should note is that pragma once is a compilation macro for VS. It is supposed to only process the header file one time per compilation unit, to avoid the already defined error. However, it does not work so great...

Instead, you should do what was mentioned in the 3 post in this thread: inclusion gaurds. Basically, it looks like this:


//CApplication.h
//<- Note this can be anything but must be unique
#ifndef CApplicationHeaderFile
#define CApplicationHeaderFile


#endif //CApplicationHeaderFile

Now, you put everything between the #define and #endif (such as more #includes or class defintions). Once the compiler reaches this, it checks to see if CApplicationHeaderFile is define, but it is not the first time. So it includes the code between the if and endif. The next time the compiler reaches this, it is defined, so it just goes to the endif. Anyway, you should always incorporate an inclusion guard into all your header files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this