auto return type of a C++ function

Heiko 1,291 Reputation points
2024-02-12T12:53:02.4666667+00:00

My last knowledge of C++ is from 1997. I am now trying to understand 'modern' C++. I have created a new C++/WinRT project in VS 2022 and inserted the following code (If you see SQL or Yaml code, I didn't mark code as SQL or YAML):

pch.h: #pragma once #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.System.h>

main.cpp: `#include "pch.h"

using namespace winrt; using namespace Windows::Foundation; using namespace winrt::Windows::Storage; using namespace winrt::Windows::System;

IAsyncAction OpenPicture();

int main() { init_apartment();

auto task { OpenPicture() };

task.get();
return 0;

}

IAsyncAction OpenPicture() { StorageFile file = co_await StorageFile::GetFileFromPathAsync(L"d:\temp\Function.png"); LauncherOptions options;

options.PreferredApplicationPackageFamilyName(L"Microsoft.Windows.Photos_8wekyb3d8bbwe");
co_await Launcher::LaunchFileAsync(file, options);

}

enter image description here

According to IntelliSense, the code is fine, but when I compile, I get the following error messages:

Error C2512 'winrt::Windows::Storage::StorageFile': no appropriate default constructor available

Error C3779 'winrt::Windows::Storage::StorageFile::GetFileFromPathAsync': a function that returns 'auto' cannot be used before it is defined WinRTTest

If I place the cursor on GetFileFromPathAsync and press F12 (Go to definition), I get the following definition:

static auto GetFileFromPathAsync(param::hstring const& path);

When I press F1 on GetFileFromPathAsync, I get the following definition in the documentation:

static IAsyncOperation<StorageFile> GetFileFromPathAsync(winrt::hstring const& path);

If I would use StorageFile * file instead of StorageFile file, I would not get the 1st compiler error.

How do I solve the compiler errors?

Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2024-02-12T16:16:02.0766667+00:00

    Try to add this directive to pch.h: #include "winrt/Windows.Storage.Streams.h".

    And use this line:

    StorageFile file = co_await StorageFile::GetFileFromPathAsync( LR"(d:\temp\Function.png)" );
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.