前提
C++で家計簿を趣味で作っています。
create_task thenからの戻り値を取得する
機能を実装中に以下の例外が発生しました。
ユニバーサルwindows アプリ (C++/CX)は最近学習を始めたばかりです。
ソースコードも見苦しいところがあると思います。
申し訳ありません。
宜しければご教授願います。
よろしくお願いします。
実現したいこと
ユニバーサルwindows アプリ (C++/CX)を利用して家計簿を作成中。
品目のデータを保存してあるファイルから読み込み、
vector<Hinmoku>を戻り値として取得したい。
ファイル読み込みにcreate_task .thenの継続処理を使っているため、
ラムダ式からの戻り値を取得する必要がある。
簡略化のために、ラムダ式からの戻り値取得の部分のみの
プロジェクトを新たに作成しました。
発生している問題・エラーメッセージ
「ハンドルされない例外が 0x00007FFF9FD17FBC (ucrtbased.dll) で発生しました(lambdaTask2.exe 内): 無効なパラメーターを 致命的と見なす関数に無効なパラメーターが渡されました。」
該当のソースコード
C++/CX
1// 2// MainPage.xaml.cpp 3// MainPage クラスの実装。 4// 5 6#include "pch.h" 7#include "MainPage.xaml.h" 8 9using namespace Platform::Collections; 10 11using namespace Concurrency; 12using namespace std; 13using namespace lambdaTask2; 14 15using namespace Platform; 16using namespace Windows::Foundation; 17using namespace Windows::Foundation::Collections; 18using namespace Windows::UI::Xaml; 19using namespace Windows::UI::Xaml::Controls; 20using namespace Windows::UI::Xaml::Controls::Primitives; 21using namespace Windows::UI::Xaml::Data; 22using namespace Windows::UI::Xaml::Input; 23using namespace Windows::UI::Xaml::Media; 24using namespace Windows::UI::Xaml::Navigation; 25 26// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください 27 28struct Hinmoku 29{ 30 long long id; 31 String^ name; 32}; 33 34task<vector<Hinmoku>> write_to_string() 35{ 36 auto s = make_shared<vector<Hinmoku>>(); 37 38 return create_task([s] 39 { 40 Hinmoku h = { 1,"食費" }; 41 (*s).push_back(h); 42 43 }).then([s] 44 { 45 Hinmoku h2 = { 2,"光熱費" }; 46 (*s).push_back(h2); 47 48 return *s; 49 }); 50} 51 52MainPage::MainPage() 53{ 54 InitializeComponent(); 55 56 auto t = write_to_string(); 57 vector<Hinmoku> v = t.get(); 58 Hinmoku hinmoku = v[0]; 59 60 Windows::UI::Popups::MessageDialog^ md = 61 ref new Windows::UI::Popups::MessageDialog( 62 "id :" + hinmoku.id + "\nname: " + hinmoku.name); 63 md->ShowAsync(); 64} 65
C++/CX
1 // 2// MainPage.xaml.h 3// MainPage クラスの宣言。 4// 5 6#pragma once 7 8#include "MainPage.g.h" 9 10namespace lambdaTask2 11{ 12 /// <summary> 13 /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 14 /// </summary> 15 public ref class MainPage sealed 16 { 17 public: 18 MainPage(); 19 20 }; 21} 22
XAML
1<Page 2 x:Class="lambdaTask2.MainPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:lambdaTask2" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d" 9 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 10 11 <Grid> 12 13 </Grid> 14</Page>
試したこと
主に使用している参考書
Visual C++2022パーフェクトマスター
参考にしたホームページ
https://docs.microsoft.com/ja-jp/cpp/parallel/concrt/task-parallelism-concurrency-runtime?view=msvc-170
タスクの並列化 (コンカレンシー ランタイム)
C++
1// lambda-task-lifetime.cpp 2// compile with: /EHsc 3#include <ppltasks.h> 4#include <iostream> 5#include <string> 6 7using namespace concurrency; 8using namespace std; 9 10task<wstring> write_to_string() 11{ 12 // Create a shared pointer to a string that is 13 // assigned to and read by multiple tasks. 14 // By using a shared pointer, the string outlives 15 // the tasks, which can run in the background after 16 // this function exits. 17 auto s = make_shared<wstring>(L"Value 1"); 18 19 return create_task([s] 20 { 21 // Print the current value. 22 wcout << L"Current value: " << *s << endl; 23 // Assign to a new value. 24 *s = L"Value 2"; 25 26 }).then([s] 27 { 28 // Print the current value. 29 wcout << L"Current value: " << *s << endl; 30 // Assign to a new value and return the string. 31 *s = L"Value 3"; 32 return *s; 33 }); 34} 35 36int wmain() 37{ 38 // Create a chain of tasks that work with a string. 39 auto t = write_to_string(); 40 41 // Wait for the tasks to finish and print the result. 42 wcout << L"Final value: " << t.get() << endl; 43} 44 45/* Output: 46 Current value: Value 1 47 Current value: Value 2 48 Final value: Value 3 49*/
上記を参考に作ったのがこちら。
一応、思った通りに動作はした。
#include <ppltasks.h> #include <iostream> using namespace concurrency; using namespace std; struct Hinmoku { long long id; string name; }; task<vector<Hinmoku>> write_to_string() { auto s = make_shared<vector<Hinmoku>>(); return create_task([s] { Hinmoku h = { 1,"a" }; (*s).push_back(h); }).then([s] { Hinmoku h2 = { 2,"b" }; (*s).push_back(h2); return *s; }); } int wmain() { auto t = write_to_string(); vector<Hinmoku> v = t.get(); cout << v[0].id << endl; cout << v[0].name << endl; }
これをユニバーサルwindowsアプリに移植したら
例外が発生して詰まってしまいました。
補足情報(FW/ツールのバージョンなど)
windows 11
visual studio 2022
ターゲットバージョン: Windows 11 (10.0;ビルド 22000)
最小バージョン Windows10, version 1809 (10.0; ビルド 17763)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。