Thomas T
Thomas T

Reputation: 99

Are you supposed to prototype IAsyncAction/IAsyncOperation coroutines (functions) in the header files?

I am new to C++/WinRT and the concurrency/thread topics for c++. I have an odd issue because I've usually create header files for my .cpp files. I ran into an error when trying to prototype my IAsyncAction coroutines in my .h file. I've reproduced the same error in a new test project. Error: E0311 cannot overload functions distinguished by return type alone Here's the .h file:

#pragma comment(lib, "windowsapp")

#include <iostream>
#include <ppltasks.h>
#include <pplawait.h>
#include <Windows.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Web.Syndication.h>

IAsyncAction DoWorkOnThreadPoolAsync(int a);
void errTest();

And the .cpp file:

#include "testing.h"

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::Syndication;
using namespace std::chrono_literals;

concurrency::task<std::wstring> RetrieveFirstTitleAsync()
{

    return concurrency::create_task([]
    {
        for (int i = 0; i < 6; i++)
        {
            Sleep(1000);
            printf("4\n");
        }
        Uri rssFeedUri{ L"https://blogs.windows.com/feed" };
        SyndicationClient syndicationClient;
        SyndicationFeed syndicationFeed{ syndicationClient.RetrieveFeedAsync(rssFeedUri).get() };
        return std::wstring{ syndicationFeed.Items().GetAt(0).Title().Text() };
    });
}

int main()
{
    winrt::init_apartment();
    printf("0\n");
    auto firstTitleOp{ DoWorkOnThreadPoolAsync(1) };
    auto countLoop{ RetrieveFirstTitleAsync() };
    printf("1 \n");
    Sleep(2000);
    printf("2 \n");
    Sleep(2000);
    printf("5 \n");

    firstTitleOp.get();
    std::wcout << countLoop.get() << std::endl;
    printf("3 \n");
}

IAsyncAction DoWorkOnThreadPoolAsync(int a)
{
    co_await winrt::resume_background(); // Return control; resume on thread pool.

    uint32_t result = 6;
    for (uint32_t x = 0; x < 15; ++x)
        {
            // Do compute-bound work here.
            co_await 400ms;
            printf("x: %d \n", x);
        }
    co_return;
}

void errTest() {
    
}

If I delete the prototype from the .h file and prototype it at the top of the .cpp, it builds. Also if I move the function above any other function that calls it, the project builds (makes sense). I'm just curious if there's a way to prototype it in the header file so I can call it from other .cpp files.

Upvotes: 0

Views: 173

Answers (1)

Damir Tenishev
Damir Tenishev

Reputation: 3272

In the h-file you didn't provide the namespace winrt::Windows::Foundation where the IAsyncAction is defined.

You have two options here: (1) move the

using namespace winrt;
using namespace Windows::Foundation;

to the header file or (2) use the full scope:

winrt::Windows::Foundation::IAsyncAction DoWorkOnThreadPoolAsync(int a);

Upvotes: 1

Related Questions