C++/CX 头文件找不到 Microsoft 命名空间

C++/CX Header file can't find Microsoft namespace

我有一个包含以下代码的头文件:

  Microsoft::WRL:ComPtr<ID3D11Device2> m_device;

在 class 定义中。 Visual Studio 2013 说 Microsoft 不是命名空间,如果我把代码剪下来,然后把它放在另一个 class 另一个文件中,保持原样,它就可以正常工作!

有什么想法吗?

菲利普

编辑:突然之间(我没有做任何更改)Intelissense 现在接受 Microsoft::WRL::ComPtry 是有效的但是当我编译它时它仍然给我错误它不存在。

You need to

#include <wrl.h>

#include <wrl/client.h>

To get Microsoft::WRL::ComPtr in your module.

When you say "Visual Studio 2013 is saying that Microsoft is not a namespace" do you mean you get a compiler error or is just Intellisense? When dealing with headers, Intellisense can get a bit out of sync until you build again.例如:

//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };

//Test.cpp
#include <wrl/client.h>
#include "Test.h"

If you just added the #include <wrl/client.h> to the Test.cpp, Intellisense might not know yet it is in scope for the header. It's perfectly valid C++ already, but a better practice is to include in your headers the ones it needs like:

//Test.h
#pragma once
#include <wrl/client.h>
class A { Microsoft::WRL::ComPtr<T> a; };

The other way this sync issue can manifest itself is if you are doing:

//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };

//Test.cpp
#include "pch.h"
#include "Test.h"

//pch.h
#include <wrl/client.h>

Again, fully valid C++ that will build. Intellisense knows it works when you build, but might not until then.

Note: WRL is traditional C++ and is not using C++/CX language extensions. They both exist to make it easier to consume WinRT APIs from C++, and you will see the Microsoft::WRL::ComPtr used inside C++/CX applications when dealing with non-WinRT COM APIs like Direct3D. And you can mix C++/CX with WRL in the same application taking advantage of the fact that you can use reinterpret_cast<> between C++/CX ref ^ and ABI COM pointers. You can use Microsoft::WRL::ComPtr in old-school Windows desktop apps on Windows 7 or Windows Vista too.

With all that said, WRL and C++/CX are two distinct things.

Update: For consuming Windows Runtime APIs, you can also use C++/WinRT which is also 'standard' C++ without any need for the C++/CX extensions.参见 Microsoft Docs。 You can use Microsoft::WRL::ComPtr for C++/WinRT applications, or you can use their variant wrl::com_ptr