Jump to content
• Advertisement

Public Group

# DX10 and C++ Class Help!

This topic is 3254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I'm a C++ noob and have been tinkering with DX10 and Qt 4.53, I have made a basic cube and now a grid that works fine. Currently the D3D stuff is in it's own class I've called it "iD3D10" It's actually subclassed from QWidget. I have seperated my functions unlike the DXSDK examples like "Vertex, Render, Viewport" just for my own readability. I would now like to try and expand this idea so i have for example a separate file(s) or class for things like "Primitives". So i made a new Class in MSVC which gave me "Primitive.c and .h" and i copied my Grid function to it, but how do i call my "Grid" function from my iD3D10 class? I always end up with errors.
void Primitive::Grid()
{
Blah = 0;
}

So i want to call the above "Grid" function in my "Primitive Class from the paint event below in my iD3D10 class.
void iD3D10::paintEvent(QPaintEvent *p)
{
Input();
//Grid();

Primitive* Primitive;
Primitive->Grid();
Viewport();
Render();
So you can see previously i just used "Grid();" and then worked fine. So I have tried to add Primitive *Primitive "Primitive->Grid(); or "Primitive.Grid(); or many variations all with various errors. I did declare "Class Primitive;" at the top of my iD3D10 file. But this is all a bit of guess work, any ideas? Any and all advice welcomed. Regards, Scott.

#### Share this post

##### Share on other sites
Advertisement
And what were the errors you saw?

In your first attempt, you called "Grid()", but there is no function named Grid(), there is a function called Primitive::Grid() -- a member of the Primitive class. Since it's not a static member, you need an instance of Primitive to call it:
Primitive primitive;primitive.Grid();

Your attempt created a pointer to a Primitive (Primitive*) which you never allocated storage for and just tried to use immediately, which likely will cause a crash.

Declaring the instance as a non-pointer should work, in terms of compilation, but it may not be semantically correct at runtime (i.e., it may not do what you want) because that depends very much on code that you are not showing us -- what Primitive looks like and does, what the rest of the iD3D10 class does, et cetera).

#### Share this post

##### Share on other sites
Quote:
 Original post by Freestyler Primitive* Primitive; Primitive->Grid();

This is trying to dereference a null pointer (or more specifically an uninitialized pointer). You've created the pointer, but not pointed it at anything, or created a new object from the pointer. You could try:

Primitive* prim = new Primitive;
or
Primitive* prim = &otherPrimitive;

and then use

prim->Grid();

or avoid the memory issues altogether and use the dot operator like JPetrie suggested.

Primitive prim;
prim.Grid();

#### Share this post

##### Share on other sites
Quote:
 Original post by jpetrieAnd what were the errors you saw?In your first attempt, you called "Grid()", but there is no function named Grid(), there is a function called Primitive::Grid() -- a member of the Primitive class. Since it's not a static member, you need an instance of Primitive to call it:Primitive primitive;primitive.Grid();

I attempted just about every combination of everything before i posted... ;)

The "//Grid();" I commented out just to show, i was previously just calling it from the same class (inline), but now i want to call it from "Primitive".

Quote:
 Your attempt created a pointer to a Primitive (Primitive*) which you never allocated storage for and just tried to use immediately, which likely will cause a crash.Declaring the instance as a non-pointer should work, in terms of compilation, but it may not be semantically correct at runtime (i.e., it may not do what you want) because that depends very much on code that you are not showing us -- what Primitive looks like and does, what the rest of the iD3D10 class does, et cetera).

I'm happy to post my code, it's i just thought the longer the message, the less chance for reply... And i really want to get the Class thing down.

Okay so if i do as you suggest i get the following errors.

1>.\iD3D10.cpp(101) : error C2065: 'Primitive' : undeclared identifier1>.\iD3D10.cpp(101) : error C2146: syntax error : missing ';' before identifier 'Prim'1>.\iD3D10.cpp(101) : error C2065: 'Prim' : undeclared identifier1>.\iD3D10.cpp(102) : error C2065: 'Prim' : undeclared identifier1>.\iD3D10.cpp(102) : error C2228: left of '.Cube' must have class/struct/union

Now, I assume the above is because i now need to have my iD3D10 header file have "#include "primitive.h"

So this causes some design issues:
//Primitive.cpp#include "Primitive.h"#include "iD3D10.h"//iD3D10.cpp#include <QWidget>#include <QtGui>#include <d3d10.h>#include <d3dx10.h>#include "Primitive.h"#include "iD3D10.h"

So i have to include the same files twice, which seems like it's always going to cause grief. Now i have Linker errors:

1>main.obj : error LNK2005: "struct ID3D10Device * g_pd3dDevice" (?g_pd3dDevice@@3PEAUID3D10Device@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct IDXGISwapChain * g_pSwapChain" (?g_pSwapChain@@3PEAUIDXGISwapChain@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10RenderTargetView * g_pRenderTargetView" (?g_pRenderTargetView@@3PEAUID3D10RenderTargetView@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10Effect * g_pEffect" (?g_pEffect@@3PEAUID3D10Effect@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10EffectTechnique * g_pTechnique" (?g_pTechnique@@3PEAUID3D10EffectTechnique@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10InputLayout * g_pVertexLayout" (?g_pVertexLayout@@3PEAUID3D10InputLayout@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10Buffer * g_pVertexBuffer" (?g_pVertexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10Buffer * g_pIndexBuffer" (?g_pIndexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10RasterizerState * g_pRasterizerState" (?g_pRasterizerState@@3PEAUID3D10RasterizerState@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pWorldVariable" (?g_pWorldVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pViewVariable" (?g_pViewVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pProjectionVariable" (?g_pProjectionVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct D3DXMATRIX g_View" (?g_View@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct D3DXMATRIX g_World" (?g_World@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>main.obj : error LNK2005: "struct D3DXMATRIX g_Projection" (?g_Projection@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10Device * g_pd3dDevice" (?g_pd3dDevice@@3PEAUID3D10Device@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct IDXGISwapChain * g_pSwapChain" (?g_pSwapChain@@3PEAUIDXGISwapChain@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10RenderTargetView * g_pRenderTargetView" (?g_pRenderTargetView@@3PEAUID3D10RenderTargetView@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10Effect * g_pEffect" (?g_pEffect@@3PEAUID3D10Effect@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10EffectTechnique * g_pTechnique" (?g_pTechnique@@3PEAUID3D10EffectTechnique@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10InputLayout * g_pVertexLayout" (?g_pVertexLayout@@3PEAUID3D10InputLayout@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10Buffer * g_pVertexBuffer" (?g_pVertexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10Buffer * g_pIndexBuffer" (?g_pIndexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10RasterizerState * g_pRasterizerState" (?g_pRasterizerState@@3PEAUID3D10RasterizerState@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pWorldVariable" (?g_pWorldVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pViewVariable" (?g_pViewVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pProjectionVariable" (?g_pProjectionVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct D3DXMATRIX g_View" (?g_View@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct D3DXMATRIX g_World" (?g_World@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>Primitive.obj : error LNK2005: "struct D3DXMATRIX g_Projection" (?g_Projection@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10Device * g_pd3dDevice" (?g_pd3dDevice@@3PEAUID3D10Device@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct IDXGISwapChain * g_pSwapChain" (?g_pSwapChain@@3PEAUIDXGISwapChain@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10RenderTargetView * g_pRenderTargetView" (?g_pRenderTargetView@@3PEAUID3D10RenderTargetView@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10Effect * g_pEffect" (?g_pEffect@@3PEAUID3D10Effect@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10EffectTechnique * g_pTechnique" (?g_pTechnique@@3PEAUID3D10EffectTechnique@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10InputLayout * g_pVertexLayout" (?g_pVertexLayout@@3PEAUID3D10InputLayout@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10Buffer * g_pVertexBuffer" (?g_pVertexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10Buffer * g_pIndexBuffer" (?g_pIndexBuffer@@3PEAUID3D10Buffer@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10RasterizerState * g_pRasterizerState" (?g_pRasterizerState@@3PEAUID3D10RasterizerState@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pWorldVariable" (?g_pWorldVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pViewVariable" (?g_pViewVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct ID3D10EffectMatrixVariable * g_pProjectionVariable" (?g_pProjectionVariable@@3PEAUID3D10EffectMatrixVariable@@EA) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct D3DXMATRIX g_View" (?g_View@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct D3DXMATRIX g_World" (?g_World@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>moc_iD3D10.obj : error LNK2005: "struct D3DXMATRIX g_Projection" (?g_Projection@@3UD3DXMATRIX@@A) already defined in iD3D10.obj1>D:\Development\Projects\MainWin\Debug\MainWin.exe : fatal error LNK1169: one or more multiply defined symbols found

And now I have 46 Linker errors, and OOP and C++ is scaring me, when all i want to do is make my code a little neater: :)All advice is welcomed.

[Edited by - Zahlman on October 28, 2009 11:02:31 PM]

#### Share this post

##### Share on other sites
Quote:
 Original post by FreestylerAnd now I have 46 Linker errors

Read this.

Quote:
 and OOP and C++ is scaring me

OOP has really nothing to do with your problems in compiling. You would have the same linker errors even without trying to create the Primitive class.

#### Share this post

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by FreestylerAnd now I have 46 Linker errors

Read this.

Yes thankyou i have read it backwards and forwards for the last 24hours, I found it from a search, Albeit divided into 4 pages, not one like your link. But the info seems exactly what i need.... But I'm still a little unsure of my next move. I will continue to read it until I understand it, I thought Extern may help but i cannot see how to apply it to my code.

Quote:
 and OOP and C++ is scaring me

OOP has really nothing to do with your problems in compiling. You would have the same linker errors even without trying to create the Primitive class.

But i don't seem to get any errors with linking or compiling my program works fine with either a Grid or a Cube drawn, when they are all from the same class.

Today i have simplified and just tried this:

//Primitive.cppint Primitive::test(){	int curIndex = 0;return curIndex;}

And then did:

//iD3D10.cppint curIndex = Prim.test();

And the correct values are passed from Primitive:Test to iD3D10:Grid, and all worked without compiler errors. So i can get the Class to work.

But if i try and expand the idea to move the whole "Grid or Cube Function to Primitive" all those D3D10 globals which are all just declared at the top of "iD3D10.cpp" cause "Issue 4" from the guide you linked too. Other stuff like "pragma once" is already used. So i assume extern needs to be provided for ALL globals? I still feel I'm missing something large...

But again i thankyou for your replies, i will read it some more and reply back.

I may post my code to get some more ideas on layout/design and OOP, I have been looking through various Nvidia, MS and other DX examples to see if others do what i do.

Thanks again.

#### Share this post

##### Share on other sites
When dealing with OOP, you basically never use globals anymore. Suppose you want to use all those globals somewhere in a project. So put them in a class / structure and hand that structure to whatever is needing them.

Of course you shouldn't simply create a class that only holds those variables, but it's a start to get you going. In the end you can create a device class, that simplifies using a D3D10 device, for example.

I hope this helps you a bit.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5
• Advertisement

• 12
• 18
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632662
• Total Posts
3007702
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!