尽管包含正确的c ++ / winrt标头,但仍出现“在定义前不能使用返回'auto'的函数”的错误

时间:2019-08-18 15:51:19

标签: c++ winrt-xaml c++-winrt

我正在尝试转换一个小型的win32桌面应用程序,以使用c ++ / winrt组件和XAML孤岛。我关注了有关该主题的大量文章,并且确实能够使用一些基本的XAML控件来编译和运行该应用程序。使用FileOpenPicker时出现我的问题,并且随后出现错误消息,指示“在定义前不能使用返回'auto'的函数”。我已经看到其他人通过包含相关的头文件来解决此问题,但是我已经做到了,但仍然遇到错误。

我已经包含了头文件; -我尝试将其包含在pch文件和源文件中。

#include "pch.h"

#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Pickers.h>

#include "360MediaPlayer.h"

using namespace winrt;
using namespace Windows::UI;
using namespace Windows::UI::Composition;
using namespace Windows::UI::Xaml::Hosting;
using namespace Windows::Foundation::Numerics;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
using namespace Windows::Media::Playback;


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define MAX_LOADSTRING 100

// Global Variables:
HWND _hWnd;
HWND _childhWnd;
HINSTANCE _hInstance;

HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    _hInstance = hInstance;

    // The main window class name.
    const wchar_t szWindowClass[] = L"Win32DesktopApp";
    WNDCLASSEX windowClass = { };

    windowClass.cbSize = sizeof(WNDCLASSEX);
    windowClass.lpfnWndProc = WndProc;
    windowClass.hInstance = hInstance;
    windowClass.lpszClassName = szWindowClass;
    windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

    //windowClass.hIconSm = LoadIcon(windowClass.hInstance, IDI_APPLICATION);

    if (RegisterClassEx(&windowClass) == NULL)
    {
        MessageBox(NULL, L"Windows registration failed!", L"Error", NULL);
        return 0;
    }

    _hWnd = CreateWindow(
        szWindowClass,
        L"Windows c++ Win32 Desktop App",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    if (_hWnd == NULL)
    {
        MessageBox(NULL, L"Call to CreateWindow failed!", L"Error", NULL);
        return 0;
    }



/*    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY360MEDIAPLAYER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    */

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY360MEDIAPLAYER));


    // The call to winrt::init_apartment initializes COM; by default, in a multithreaded apartment.
    winrt::init_apartment(apartment_type::single_threaded);

    // Initialize the Xaml Framework's corewindow for current thread
    WindowsXamlManager winxamlmanager = WindowsXamlManager::InitializeForCurrentThread();

    // This DesktopWindowXamlSource is the object that enables a non-UWP desktop application 
    // to host UWP controls in any UI element that is associated with a window handle (HWND).
    DesktopWindowXamlSource desktopSource;
    // Get handle to corewindow
    auto interop = desktopSource.as<IDesktopWindowXamlSourceNative>();
    // Parent the DesktopWindowXamlSource object to current window
    check_hresult(interop->AttachToWindow(_hWnd));

    // This Hwnd will be the window handler for the Xaml Island: A child window that contains Xaml.  
    HWND hWndXamlIsland = nullptr;
    // Get the new child window's hwnd 
    interop->get_WindowHandle(&hWndXamlIsland);
    // Update the xaml island window size becuase initially is 0,0
    SetWindowPos(hWndXamlIsland, 0, 200, 100, 800, 200, SWP_SHOWWINDOW);

    //Creating the Xaml content
    Windows::UI::Xaml::Controls::StackPanel xamlContainer;
    xamlContainer.Background(Windows::UI::Xaml::Media::SolidColorBrush{ Windows::UI::Colors::LightGray() });

    Windows::UI::Xaml::Controls::TextBlock tb;
    tb.Text(L"Hello World from Xaml Islands!");
    tb.VerticalAlignment(Windows::UI::Xaml::VerticalAlignment::Center);
    tb.HorizontalAlignment(Windows::UI::Xaml::HorizontalAlignment::Center);
    tb.FontSize(48);

    MediaPlayer mpSphere = MediaPlayer();


    FileOpenPicker foPicker = FileOpenPicker();
    StorageFile file(foPicker.PickSingleFileAsync().get());

    mpSphere.SetFileSource(file);
    mpSphere.Play();

//  xamlContainer.Children().Append(tb);
    xamlContainer.UpdateLayout();
    desktopSource.Content(xamlContainer);

    //End XAML Island section

    ShowWindow(_hWnd, nCmdShow);
    UpdateWindow(_hWnd);


    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

因此,我得到的完整错误消息如下:

  

错误C3779'winrt :: impl :: consume_Windows_Foundation_IAsyncOperation ,TResult> :: get':返回'auto'的函数'不能在定义前使用'

尽管存在头文件,如上所示。如果删除与媒体文件有关的代码,仅保留XAML内容,则它将运行。有人知道我想念什么吗?如果需要,我可以提供完整的项目。

1 个答案:

答案 0 :(得分:1)

auto这个关键字很简单,要求编译器在编译时使用您要使用的类型来推断类型,例如从函数返回的值或像int这样的硬编码POD类型。

您的代码存在问题,因为您缺少Winrt Foundation的包含,这是您的代码所必需的,因为它访问Async函数,老实说,大多数WinRT代码都需要包含Foundation的

#include <winrt/Windows.Foundation.h>

using namespace winrt;
using namespace Windows::Foundation;

由于您忘记了包括基础include,因此编译器无法推断出引发错误的类型.....

请注意,auto关键字非常适合以尾随返回类型形式使用的函数,但是我强烈建议您不要出于多种原因而将它们用于变量,一些值得注意的原因是带有矢量迭代器的奇怪错误,其中即使正确包含在模板函数中,auto关键字也无法推断类型并会导致错误,

template <typename T>
auto findSomething(:std::string name)
    -> T*
{
    // the code here causes an error since we are trying to deduce the var inside a 
    // template function using auto
    auto _Found = ::std::find_if(somevector.begin(), somevector.end(), [&](::std::pair<::std::string, ::std::unique_ptr<class Someclass>>& pair){ return (pair.first = name) ? true : false; });

    if(_Found != somevector.end())
    {
        return static_cast<T*>(_Found->second.get());
    }
    return nullptr;
}

更正

template <typename T>
auto findSomething(:std::string name)
    -> T*
{
    ::std::vector<::std::pair<::std::string, ::std::unique_ptr<class Someclass>>>::iterator _Found = ::std::find_if(somevector.begin(), somevector.end(), [&](::std::pair<::std::string, ::std::unique_ptr<class Someclass>>& pair){ return (pair.first = name) ? true : false; });

    if(_Found != somevector.end())
    {
        return static_cast<T*>(_Found->second.get());
    }
    return nullptr;
}

如果您不想每次都输入::std::vector<::std::pair<::std::string, ::std::unique_ptr<class someclass>>>::iterator,只需用using关键字using myvectortype = ::std::vector<::std::pair<::std::string, ::std::unique_ptr<class someclass>>>键入别名即可,您只需要输入myvectortype::iterator。...

希望这可以帮助您修复代码。