-3

I'm making a DirectX11 App in Visual Studio 2017 using C++, I need to declare a data structure in my "GeometryGenerator.h" header file.

The problem is that when I try to use the types: XMFLOAT3 in my header file I got an error from Visual Studio when I try to run the project and gives me this message:

“C4430: missing type specifier - int assumed”

In the lines where I declare variables of type XMFLOAT3

This is my code:

#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();

    };

}

If I add this:

using namespace DirectX;

I get rid of the problem. My question is whether using namespaces X in header files a bad and dangerous practice in C++? And also how then can I use types declared in their own namespaces inside .cpp files?

6
  • 3
    Use DirectX::XMFLOAT3 instead of XMFLOAT3
    – drescherjm
    Sep 7, 2018 at 17:03
  • 4
    Using namespaces foo is bad to do in header files, especially if you have multiple namespaces, as this would defeat the purpose of namespaces. DirectX::XMFLOAT3 is a correct way Sep 7, 2018 at 17:04
  • 1
    en.cppreference.com/w/cpp/language/namespace for reference Sep 7, 2018 at 17:06
  • It's best to always copy the exact error message (instead of paraphrasing) from the Output Tab (yes the output tab not the errors list).
    – drescherjm
    Sep 7, 2018 at 17:06
  • "How can I use types declared in namespaces inside .cpp files?" - you simply use their full name. For example DirectX::XMFLOAT3. Sep 7, 2018 at 17:28

1 Answer 1

5

How can I use types declared in namespaces inside .cpp files?

You use the scope resolution operator, like this in your case:

Vertex(const DirectX::XMFLOAT3& p, const DirectX::XMFLOAT3& n, const DirectX::XMFLOAT3& t, const DirectX::XMFLOAT2& uv)

...

DirectX::XMFLOAT3 Position;
DirectX::XMFLOAT3 Normal;
DirectX::XMFLOAT3 TangentU;
DirectX::XMFLOAT2 TexC;

and so on for every time you are referring to a name in that namespace.

A second option, in case you really wish to avoid typing the namespace per each usage in the header file is using the following using syntax:

using XMFLOAT3 = DirectX::XMFLOAT3;

By aliasing like this, you can use whatever name you put before the = instead of the whole DirectX::XMFLOAT3. But do take into consideration that if you do so in a relatively global context (such as file or enscoping namespace) then anyone familiar with the namespace, DirectX for example, would have to remember your aliases when going over your code, as opposed to the quite recognizable and unambiguous first option of specifying the full name.

but Is adding namespaces in header files a bad and dangerous practice in C++?

Indeed, see comprehensive explanation here: Why is “using namespace std” considered bad practice?.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.