question

Roongroj avatar image
0 Votes"
Roongroj asked ·

[UWP][C++/WinRT] Please advise how to code without error for ReadAsync.

I'm trying to find StorageFile's OpenAsync, ReadAsync for C++/WinRT very difficult. ( and tried testing with C # in UWP, it passed no problem. I originally developed with C ++ MFC. )

Windows::Foundation::IAsyncAction MainPage::FilePickerAsync()
{
auto picker = FileOpenPicker();

 picker.ViewMode( PickerViewMode::List ); 

 picker.FileTypeFilter().Append( L".cpp" ); 

 StorageFile file = co_await picker.PickSingleFileAsync(); 

 try 
 { 
         IRandomAccessStream stream = co_await file.OpenAsync( FileAccessMode::Read ); 

         uint64_t size64 = stream.Size();                                        // <= Error 

         uint32_t size32 = static_cast< uint32_t >( size64 );  

         auto buffer = Buffer( size32 ); 

         co_await stream.ReadAsync( buffer, size32, InputStreamOptions::None );  // <= Error 

         stream.Close();  
 } 
 catch ( const hresult_error &ex ) 
 { 
 } 

}

Visual C++ 2019

Error C3779 'winrt::impl::consume_Windows_Storage_Streams_IRandomAccessStream <<winrt::Windows::Storage::Streams::IRandomAccessStream>>::Size': a function that returns 'auto' cannot be used before it is defined

Error C3779 'winrt::impl::consume_Windows_Storage_Streams_IInputStream<< D >>::ReadAsync': a function that returns 'auto' cannot be used before it is defined

windows-uwpc++windows-uwp-runtime
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered ·

Hello, Welcome to Micorosoft Q&A,


a function that returns 'auto' cannot be used before it is defined

Derive from Raymond Chen's blog,

The problem is that you are missing the header file for the interface you are using. In this case, we are using Windows.Foundation.Collections.IIterable, so we need to include

 #include <winrt/Windows.Foundation.Collections.h>


And the key point is that the forward-declared methods are declared as returning auto with no trailing return type and no body. This means “I want the compiler to deduce the return type (but I’m not giving any clues yet).” If you try to call the method before the method has been implemented, then the compiler reports an error because it doesn’t yet have the necessary information to determine the return type.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. #include <winrt/Windows.Foundation.Collections.h> is default of new project, and I add using namespace Windows::Foundation::Collections; but same error as before.


0 Votes 0 ·

You need all the specific head file then add the namespace.


pch file


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

MainPage namespace


 using namespace Windows::Storage::Pickers;
 using namespace Windows::Storage::Streams;
 using namespace Windows::Storage;

1 Vote 1 ·

It's complete, Thank you very much.

include <winrt/Windows.Storage.Streams.h> // I don't add.

1 Vote 1 ·