【发布时间】:2018-09-07 17:00:34
【问题描述】:
我正在使用 C++ 在 Visual Studio 2017 中制作 DirectX11 应用程序,我需要在“GeometryGenerator.h”头文件中声明一个数据结构。
问题是,当我尝试在头文件中使用类型:XMFLOAT3 时,当我尝试运行项目时,Visual Studio 出现错误并给我以下消息:
“C4430: missing type specifier - int assumed”
在我声明 XMFLOAT3 类型变量的行中
这是我的代码:
#pragma once
#include "..\Common\DeviceResources.h"
#include "ShaderStructures.h"
#include "..\Common\StepTimer.h"
namespace DirectX11Engine
{
class GeometryGenerator {
public:
struct Vertex
{
Vertex() {}
Vertex(const XMFLOAT3& p, const XMFLOAT3& n, const XMFLOAT3& t, const XMFLOAT2& uv)
: Position(p), Normal(n), TangentU(t), TexC(uv) {}
Vertex(
float px, float py, float pz,
float nx, float ny, float nz,
float tx, float ty, float tz,
float u, float v)
: Position(px, py, pz), Normal(nx, ny, nz),
TangentU(tx, ty, tz), TexC(u, v) {}
XMFLOAT3 Position;
XMFLOAT3 Normal;
XMFLOAT3 TangentU;
XMFLOAT2 TexC;
};
void PruebaDeTipos();
};
}
如果我添加这个:
using namespace DirectX;
我摆脱了这个问题。我的问题是头文件中的using namespaces X 在 C++ 中是否是一种糟糕且危险的做法?还有我怎样才能在 .cpp 文件中使用在它们自己的命名空间中声明的类型?
【问题讨论】:
-
使用
DirectX::XMFLOAT3而不是XMFLOAT3 -
在头文件中使用名称空间 foo 是不好的,特别是如果您有多个名称空间,因为这会破坏名称空间的用途。 DirectX::XMFLOAT3 是正确的方式
-
最好总是从输出选项卡(是的输出选项卡不是错误列表)复制确切的错误消息(而不是解释)。
-
"如何使用在 .cpp 文件中的命名空间中声明的类型?" - 您只需使用他们的全名。例如
DirectX::XMFLOAT3。
标签: c++ header namespaces directx-11