views:

76

answers:

3

I am getting confused on why the compiler is not recognizing my classes. So I am just going to show you my code and let you guys decide. My error is this

error C2653: 'RenderEngine' : is not a class or namespace name

and it's pointing to this line

std::vector<RenderEngine::rDefaultVertex> m_verts;

Here is the code for rModel, in its entirety. It contains the varible. the class that holds it is further down.

#ifndef _MODEL_H
#define _MODEL_H
#include "stdafx.h"
#include <vector>
#include <string>
//#include "RenderEngine.h"
#include "rTri.h"

class rModel {
public:

    typedef tri<WORD> sTri;

    std::vector<sTri> m_tris;
    std::vector<RenderEngine::rDefaultVertex> m_verts;

    std::wstring m_name;

    ID3D10Buffer *m_pVertexBuffer;
    ID3D10Buffer *m_pIndexBuffer;

    rModel( const TCHAR *filename );
    rModel( const TCHAR *name, int nVerts, int nTris );

    ~rModel();

    float GenRadius();
    void Scale( float amt );
    void Draw();

    //------------------------------------ Access functions.
    int NumVerts(){ return m_verts.size(); }
    int NumTris(){ return m_tris.size(); }
    const TCHAR *Name(){ return m_name.c_str(); }

    RenderEngine::cDefaultVertex *VertData(){ return &m_verts[0]; }
    sTri *TriData(){ return &m_tris[0]; }

};

#endif

at the very top of the code there is a header file

#include "stdafx.h"

that includes this

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "resource.h"
#include "d3d10.h"
#include "d3dx10.h"
#include "dinput.h"
#include "RenderEngine.h"
#include "rModel.h"


// TODO: reference additional headers your program requires here

as you can see, RenderEngine.h comes before rModel.h

#include "RenderEngine.h"
    #include "rModel.h"

According to my knowledge, it should recognize it. But on the other hand, I am not really that great with organizing headers. Here my my RenderEngine Declaration.

#pragma once
#include "stdafx.h"



#define MAX_LOADSTRING 100
#define MAX_LIGHTS 10

class RenderEngine {
public:
    class rDefaultVertex
    {
    public:
        D3DXVECTOR3 m_vPosition;  
        D3DXVECTOR3 m_vNormal;
        D3DXCOLOR m_vColor;
        D3DXVECTOR2 m_TexCoords;
    };

    class rLight
    {
    public:
        rLight()
        {

        }
        D3DXCOLOR m_vColor;
        D3DXVECTOR3 m_vDirection;
    };

    static HINSTANCE m_hInst;
    HWND m_hWnd;
    int m_nCmdShow;
    TCHAR m_szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR m_szWindowClass[MAX_LOADSTRING];          // the main window class name

    void DrawTextString(int x, int y, D3DXCOLOR color, const TCHAR *strOutput);

    //static functions
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

    bool InitWindow();
    bool InitDirectX();
    bool InitInstance();
    int Run();
    void ShutDown();

    void AddLight(D3DCOLOR color, D3DXVECTOR3 pos);

    RenderEngine()
    {
        m_screenRect.right = 800;
        m_screenRect.bottom = 600;
        m_iNumLights = 0;
    }
protected:

    RECT m_screenRect;

    //direct3d Members
    ID3D10Device *m_pDevice; // The IDirect3DDevice10
    // interface
    ID3D10Texture2D *m_pBackBuffer; // Pointer to the back buffer
    ID3D10RenderTargetView *m_pRenderTargetView; // Pointer to render target view
    IDXGISwapChain *m_pSwapChain; // Pointer to the swap chain
    RECT m_rcScreenRect; // The dimensions of the screen

    ID3D10Texture2D *m_pDepthStencilBuffer;
    ID3D10DepthStencilState *m_pDepthStencilState;
    ID3D10DepthStencilView *m_pDepthStencilView;

    //transformation matrixs system
    D3DXMATRIX m_mtxWorld;
    D3DXMATRIX m_mtxView;
    D3DXMATRIX m_mtxProj;

    //pointers to shaders matrix varibles
    ID3D10EffectMatrixVariable* m_pmtxWorldVar;
    ID3D10EffectMatrixVariable* m_pmtxViewVar;
    ID3D10EffectMatrixVariable* m_pmtxProjVar;

    //Application Lights
    rLight m_aLights[MAX_LIGHTS]; // Light array
    int m_iNumLights; // Number of active lights

    //light pointers from shader
    ID3D10EffectVectorVariable* m_pLightDirVar;
    ID3D10EffectVectorVariable* m_pLightColorVar;
    ID3D10EffectVectorVariable* m_pNumLightsVar;

    //Effect members
    ID3D10Effect *m_pDefaultEffect;
    ID3D10EffectTechnique *m_pDefaultTechnique;
    ID3D10InputLayout* m_pDefaultInputLayout;

    ID3DX10Font *m_pFont; // The font used for rendering text
    // Sprites used to hold font characters
    ID3DX10Sprite *m_pFontSprite;

    ATOM RegisterEngineClass();
    void DoFrame(float);
    bool LoadEffects();
    void UpdateMatrices();
    void UpdateLights();

};

The classes are defined within the class

class rDefaultVertex
        {
        public:
            D3DXVECTOR3 m_vPosition;  
            D3DXVECTOR3 m_vNormal;
            D3DXCOLOR m_vColor;
            D3DXVECTOR2 m_TexCoords;
        };

        class rLight
        {
        public:
            rLight()
            {

            }
            D3DXCOLOR m_vColor;
            D3DXVECTOR3 m_vDirection;
        };

Not sure if thats good practice, but I am just going by the book.

In the end, I just need a good way to organize it so that rModel recognizes RenderEngine. and if possible, the other way around.

[edit]

I can literally just point to the render engine class, and it still won't recognize

#ifndef _MODEL_H
#define _MODEL_H
//#include "stdafx.h"
#include <vector>
#include <string>
#include "RenderEngine.h"  //<-------pointing to render engine. still does not recognize.
#include "rTri.h"

class rModel {
public:

    typedef tri<WORD> sTri;

    std::vector<sTri> m_tris;
    std::vector<RenderEngine::rDefaultVertex> m_verts;

    std::wstring m_name;

    ID3D10Buffer *m_pVertexBuffer;
    ID3D10Buffer *m_pIndexBuffer;

    rModel( const TCHAR *filename );
    rModel( const TCHAR *name, int nVerts, int nTris );

    ~rModel();

    float GenRadius();
    void Scale( float amt );
    void Draw();

    //------------------------------------ Access functions.
    int NumVerts(){ return m_verts.size(); }
    int NumTris(){ return m_tris.size(); }
    const TCHAR *Name(){ return m_name.c_str(); }

    RenderEngine::cDefaultVertex *VertData(){ return &m_verts[0]; }
    sTri *TriData(){ return &m_tris[0]; }

};

#endif
A: 

It's pretty hard to understand all your dependencies and I think that your code needs refactoring (if not rewriting). Also, rushing into a rendering engine development without any architecture building experience (as I assume) generally results in crappy unreliable code, hacks "to make it work" and other evil stuff.

Anyway, in this case, try to decompose your application's parts and distinguish them. In your case, one of possible approaches would mean putting all rendering objects / structs such as Vertex, Light, Model in another header (you could call it render.types.h or render.objects.h or something like that).

Then you should simply make your rendering engine work with these render objects. Once you're done with it, you won't need statements like std::vector<RenderEngine::rDefaultVertex> m_verts; or RenderEngine::cDefaultVertex *VertData().

Note that this would also solve your circular dependencies, because your engine would only have to know about models, models - only about vertexes and so on.

If you still face the dependency troubles, use forward declarations. Google it for full description, and just a sample for you to understand what I mean:

class ForwardDeclared;

class Object {
    ForwardDeclared* member;
    void Method(const ForwardDeclared& fd) { (...) }
};

// This could also resude in another file
class ForwardDeclared { (...) };
Kotti
Well, my application is not that complex. It only consist of 2 classes. RenderEngine(with rLight and rDefaultVertex) and rMdodel. That is it. Of course there is the WinMain document but has stdafx header file as well. In a nutshell, all my documents have a stdafx header. There's no direct way at looking at this and saying what is right and what is wrong ?
numerical25
Start an `Empty Project`, add all your headers and get rid of precompiled header `stdafx.h`. After you do this, resolving your file issue would become a simple task.
Kotti
A: 

I can't tell for sure, but RenderEngine.h probably includes rModel.h. Then when you include stdafx.h it brings in RenderEngine.h, but before it fully processes that header it then includes rModel.h. Then inside rModel.h the include guards prevent it from including stdafx.h again and it proceeds to compile rModel.h without full knowledge of anything in RenderEngine.h.

Without understanding the full relationship of your various classes its extremely hard to suggest a fix. Most likely you'll want to carefully consider which classes are defined in which headers and use forward declarations to remove the circular dependency.

Mark B
+1  A: 

As others mentioned, a decoupling/refactoring is in order here - stdafx.h is not meant to hold all header files your application has.

Your problem is that renderengine.h also includes stdafx.h. This time the renderengine.h include is ignored due to #pragma once and rmodel.h is included next - at the top of renderengine.h.

The simplest thing in your case would be to:

  • remove renderengine.h & rmodel.h from stdafx.h
  • rmodel.h includes renderengine.h
  • ... done
Georg Fritzsche