Constructing a Class within another Class

Hi guys

I have a problem which for I can't seem to find a work around that I'm happy with.

I have two Classes, I'm trying to Construct the second class within the first class, but the second class has one argument, this argument is actually defined in the second class, but it's not working...

What I need is for my RENDER class to pass a structure onto the VERTHANDLER class, but this argument is used in the RENDER class, but is also needed in the VERTHANDLER's class construction argument.

Sorry in advanced for long post!
Also: If you see any bad styling, just tip me off :P

Line 25 in Class 1 Header is where I'm having trouble...it gives me this error:

syntax error : identifier 'd3ddev'

-----------------------------------CODE-----------------------------------


Class 1 (header) [renderer.h]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#ifndef RENDERER_H
#define RENDERER_H

#include <d3d9.h>
#include <d3dx9.h>
#include "vertHandler.h"

// Direct 3D Library
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// Global Defines
#define RES_WIDTH 500
#define RES_HEIGHT 500


class RENDERER
{
public:
	// Declarations
	LPDIRECT3D9 d3d; // Pointer to Direct3D interface
	LPDIRECT3DDEVICE9 d3ddev; // Pointer to the device class

	VERTHANDLER vertHandler(d3ddev);

	// Forward declaration of functions
	void initD3D(HWND hWnd); // Sets up and initializes Direct3D
};

#endif 


Class 1 (source) [renderer.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"

#include "renderer.h"
#include <d3d9.h>
#include <d3dx9.h>
#include "vertHandler.h"

// Direct 3D Library
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")



// ===========================================================================================================
// =================== Functions =============================================================================
// ===========================================================================================================

// Initialize and prepare Direct3D for use
void RENDERER::initD3D(HWND hWnd)
{
	// Instantiate a new VERTHANDLER

    d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp;  // Create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));  // Clear out the struct for use
    d3dpp.Windowed = TRUE;  // Program windowed, not fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;  // Discard old frames
    d3dpp.hDeviceWindow = hWnd;  // Set the window to be used by Direct3D
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // Set the back buffer format to 32-bit
	d3dpp.BackBufferHeight = RES_HEIGHT;
	d3dpp.BackBufferWidth = RES_WIDTH;

    // Create a device class using this information and information from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

	vertHandler.writeBuffer(); // Write vertices to memory when initialising D3D

	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // Turn 3D lighting off

	// Draw two sided polies (Note: You have to disengage cullmode before rendering two sided polies
	d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	d3ddev->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
}


Class 2 (Header) [vertHandler.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef VERTHANDLER_H
#define VERTHANDLER_H

#include <d3d9.h>
#include <d3dx9.h>

// Direct 3D Library
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// Custom Vertex Format
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE) // 3D

struct CUSTOMVERT
{
	FLOAT x, y, z;
	DWORD color; // [Binary] From the D3DFVF_DIFFUSE flag
};


// Function forward declarations
class VERTHANDLER
{
	LPDIRECT3DDEVICE9 d3ddev;
public:
	// Constructor
	VERTHANDLER(LPDIRECT3DDEVICE9);

	void writeBuffer(void);
	void cleanBuffer(void);

	// Declare pointer to vertex buffer
	LPDIRECT3DVERTEXBUFFER9 v_buffer;
};

// Constructor
VERTHANDLER::VERTHANDLER(LPDIRECT3DDEVICE9 aD3DDev)
{
	d3ddev = aD3DDev;
}

#endif 


Class 2 (Source) [vertHandler.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"
#include "vertHandler.h"

// ===========================================================================================================
// =================== Functions =============================================================================
// ===========================================================================================================

// Write vertex list to memory
void VERTHANDLER::writeBuffer(void)
{
    // Create three vertices using the CUSTOMVERTEX struct built earlier
	// Makes a triangle fyi
    CUSTOMVERT vertices[] =
    {
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
    };

    // Create the vertex and store the pointer into v_buffer, which is created globally
    d3ddev->CreateVertexBuffer(3 * sizeof(CUSTOMVERT), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

    VOID* pVoid;    // The void pointer

	// At this point, lock the memory so it doesn't move around and insert
	// list of vertices there, then unlock when done
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);    // lock the vertex buffer
    memcpy(pVoid, vertices, sizeof(vertices));    // copy the vertices to the locked buffer
    v_buffer->Unlock();    // unlock the vertex buffer
}

void VERTHANDLER::cleanBuffer(void)
{
	v_buffer->Release();
}
Well, you asked for it:

Don't use pragmas to link to libraries.

Don't write your names all in caps.

Don't randomly indent code that's not logically separate from the rest.

Don't comment everything. A comment like this:

 
v_buffer->Unlock();    // unlock the vertex buffer 


Is like

 
buy_bread(); //buys bread 


It doesn't add anything, and just serves as a sore to the eye of the reader.


Same with

1
2
3
4

// ===========================================================================================================
// =================== Functions =============================================================================
// =========================================================================================================== 


And pretty much almost every single other instance of you commenting something in the code you showed us.

Also, now might not be the best of times to get started with d3d9 - we ARE at d3d11 already after all, and it's overall much nicer than d3d9 (though I'm still quite amazed at how MS came to the decision to put D3D in front of everything instead of just putting their code into a namespace... But that's a different topic entirely).


As to your actual problem:

1
2

	VERTHANDLER vertHandler(d3ddev);


is not valid C++. You need to do this in the initializer list of your constructor. Consult google if you haven't heard the term before.
Last edited on
Topic archived. No new replies allowed.