我对为什么编译器无法识别我的类感到困惑。因此,我将向您展示我的代码,并让大家决定。我的错误是这样

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

它指向这条线
std::vector<RenderEngine::rDefaultVertex> m_verts;

这是rModel的全部代码。它包含变量。持有它的类(Class)进一步下降。
#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

在代码的最顶部,有一个头文件
#include "stdafx.h"

包括这个
// 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

如您所见,RenderEngine.h在rModel.h之前
#include "RenderEngine.h"
    #include "rModel.h"

据我所知,它应该认识到它。但是,另一方面,组织标题并不是很好。这是我的RenderEngine声明。
#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();

};

类在类内定义
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;
        };

不知道这是否是一个好习惯,但是我只是顺其自然。

最后,我只需要一种很好的组织方式,以便rModel识别RenderEngine。如果可能的话,反之亦然。

[编辑]

我可以直接指向渲染引擎类,但它仍然无法识别
#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

最佳答案

就像其他人提到的那样,这里需要进行解耦/重构- stdafx.h并不意味着保留应用程序具有的所有头文件

您的问题是renderengine.h也包括stdafx.h。这次由于renderengine.h而忽略了#pragma once包含,并且下一步将rmodel.h包含在renderengine.h的顶部。

您的情况中最简单的事情是:

  • renderengine.h删除rmodel.hstdafx.h
  • rmodel.h包括renderengine.h
  • ...完成
  • 关于c++ - header 困惑。编译器无法识别数据类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2897266/

    10-08 22:49