我正在尝试编写一个简单的DirectX11引擎的代码,但是我一直遇到这个奇怪的错误,但我找不到问题所在:我定义了Terrain类和Mesh类,并在Terrain类中#include Mesh类:

Terrain类的定义:

// Terrain.h
#pragma once

#include "Noise.h"
#include "Mesh.h"

class Terrain
{
public:
    Terrain(float width, float depth, int numVerticesW, int numVerticesD);
    ~Terrain();
    float GetHeight(float x, float z);
    void Draw();
private:
    Mesh mMesh;                     // I get the error on this line
    Noise mNoiseGenerator;
    std::vector<float> mHeights;
    void CreateTerrain(float width, float depth, int numVerticesW, int numVerticesD);
    float ComputeHeight(float x, float z, float startFrequency, float startAmplitude, float persistence, int octaves);
};

和Mesh类定义:
// Mesh.h
#pragma once

#include <d3d11.h>
#include <vector>
#include "Game.h"

class Mesh
{
public:
    Mesh();
    ~Mesh();
    template <typename T, unsigned int N>
    void LoadVertexBuffer(T data[][N], unsigned int size, bool dynamic = false);
    void LoadIndexBuffer(std::vector<unsigned int> indices);
    void SetVertexCount(unsigned int vertexCount);
    void Bind();
    void Draw();
private:
    std::vector<ID3D11Buffer*> mVertexBuffers;
    std::vector<unsigned int> mStrides;
    ID3D11Buffer *mIndexBuffer;
    unsigned int mVertexCount;
};


template <typename T, unsigned int N>
void Mesh::LoadVertexBuffer(T data[][N], unsigned int size, bool dynamic)
{
    D3D11_BUFFER_DESC bufferDesc = {};
    bufferDesc.Usage = dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_IMMUTABLE;
    bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bufferDesc.ByteWidth = sizeof(T[N]) * size;
    bufferDesc.CPUAccessFlags = dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
    bufferDesc.MiscFlags = 0;
    bufferDesc.StructureByteStride = 0;

    D3D11_SUBRESOURCE_DATA bufferData = {};
    bufferData.pSysMem = data;

    ID3D11Buffer *buffer;
    Game::GetInstance()->GetDevice()->CreateBuffer(&bufferDesc, &bufferData, &buffer);
    mVertexBuffers.push_back(buffer);
    mStrides.push_back(sizeof(T[N]));
}

当我编译代码时,我得到:
Severity    Code    Description Project File    Line    Suppression State
Error   C3646   'mMesh': unknown override specifier DirectX11 engine 0.3    c:\users\luca\desktop\programming\code\c++\source\visual studio\directx11 engine 0.3\terrain.h  14
Severity    Code    Description Project File    Line    Suppression State
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    DirectX11 engine 0.3    c:\users\luca\desktop\programming\code\c++\source\visual studio\directx11 engine 0.3\terrain.h  14

我在网上搜索,但大多数结果显示缺少分号或循环包含问题,但找不到任何问题。

编辑
我找到了问题,但无法解释为什么我的解决方案有效:
遵循包含树:
Terrain.h-> Mesh.h-> Game.h-> Renderer.h-> Terrain.h

消除#include“Terrain.h”(因为我只是在类中声明Terrain *指针)并将其添加到Terrain.cpp似乎可以解决此问题。
因此,这一定是循环包含的问题,但是我不应该通过使用 header /包含防护来防止这种情况吗?

最佳答案

您的问题是#pragma once仅防止双重包含。 IE。它使以下内容变得安全(简化后变得显而易见):

// Terrain.cpp
#include "Terrain.h"
#include "Terrain.h"

它不能解决循环包含问题,而要自动解决则要困难得多。通过双重包容,很明显哪个是第一个。但是一个圈子没有开始。

关于c++ - VisualC++编译器错误C3646 : unknown override specifier,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49752720/

10-17 00:37