質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
GCC

GCCはGNU Compiler Collectionの略です。LinuxのC言語コンパイラのデファクトスタンダードであり、数多くの他言語やプラットフォームサポートもします。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Q&A

解決済

3回答

5941閲覧

C++11のasyncを使って非同期処理をしたいがコンパイルエラー

mao999

総合スコア111

GCC

GCCはGNU Compiler Collectionの略です。LinuxのC言語コンパイラのデファクトスタンダードであり、数多くの他言語やプラットフォームサポートもします。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

0グッド

0クリップ

投稿2016/01/23 17:51

編集2016/01/24 15:48

C++11のasyncを使って非同期処理をしたいのですが、コンパイルエラーとなり上手くいきません。
おそらく初歩的な所でミスをしていると感じているのですが、STLのエラー文が私には分かり辛くて困っています。
どなたか解決策を教えて頂けますでしょうか。

#環境
Raspberry Pi 2 Model B

$uname -a
Linux rpi01 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux

$gcc -v
組み込み spec を使用しています。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/armv7l-unknown-linux-gnueabihf/5.2.0/lto-wrapper
ターゲット: armv7l-unknown-linux-gnueabihf
configure 設定: ../configure --with-fpu=vfp --with-float=hard --disable-bootstrap --enable-languages=c,c++
スレッドモデル: posix
gcc バージョン 5.2.0 (GCC)

#ソース

lang

1#include <functional> 2#include <future> 3 4using namespace std; 5struct A 6{ 78 vector<future<int>> async_result; 910}

この宣言をするだけでエラーが出ます。
コンパイル時のオプションに、-std=c++11 -lpthread は付けています。

asyncで処理させる関数の戻り値を得たい為、future<int>としています。

#エラー文

In file included from /usr/local/include/c++/5.2.0/vector:64:0, from /usr/local/include/c++/5.2.0/bits/random.h:34, from /usr/local/include/c++/5.2.0/random:49, from /usr/local/include/c++/5.2.0/bits/stl_algo.h:66, from /usr/local/include/c++/5.2.0/algorithm:62, from /usr/local/include/opencv2/hal/defs.h:572, from /usr/local/include/opencv2/core/cvdef.h:59, from /usr/local/include/opencv2/core.hpp:52, from /usr/local/include/opencv2/core/core.hpp:48, from mymonitor_main.cpp:9: /usr/local/include/c++/5.2.0/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = std::future<int>; _Alloc = std::allocator<std::future<int> >]’: /usr/local/include/c++/5.2.0/bits/stl_vector.h:257:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = std::future<int>; _Alloc = std::allocator<std::future<int> >]’ mymonitor_main.cpp:86:8: required from here /usr/local/include/c++/5.2.0/bits/stl_vector.h:161:9: エラー: invalid use of incomplete type ‘class std::future<int>’ - this->_M_impl._M_start); } ^ In file included from mymonitor_main.cpp:78:0: /usr/local/include/c++/5.2.0/future:115:11: 備考: declaration of ‘class std::future<int>’ class future; ^ In file included from /usr/local/include/c++/5.2.0/bits/stl_tempbuf.h:60:0, from /usr/local/include/c++/5.2.0/bits/stl_algo.h:62, from /usr/local/include/c++/5.2.0/algorithm:62, from /usr/local/include/opencv2/hal/defs.h:572, from /usr/local/include/opencv2/core/cvdef.h:59, from /usr/local/include/opencv2/core.hpp:52, from /usr/local/include/opencv2/core/core.hpp:48, from mymonitor_main.cpp:9: /usr/local/include/c++/5.2.0/bits/stl_construct.h: In instantiation of ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::future<int>*]’: /usr/local/include/c++/5.2.0/bits/stl_construct.h:151:15: required from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_Tp>&) [with _ForwardIterator = std::future<int>*; _Tp = std::future<int>]’ /usr/local/include/c++/5.2.0/bits/stl_vector.h:424:22: required from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = std::future<int>; _Alloc = std::allocator<std::future<int> >]’ mymonitor_main.cpp:86:8: required from here /usr/local/include/c++/5.2.0/bits/stl_construct.h:127:11: エラー: invalid use of incomplete type ‘std::iterator_traits<std::future<int>*>::value_type {aka class std::future<int>}’ __destroy(__first, __last); ^ In file included from mymonitor_main.cpp:78:0: /usr/local/include/c++/5.2.0/future:115:11: 備考: declaration of ‘std::iterator_traits<std::future<int>*>::value_type {aka class std::future<int>}’ class future; ^

宜しくお願い致します。


2016/01/25
事態が進展した為、情報を追加します。
実際のソースコードでは、

lang

1#include <functional> 2#include <future> 3#include <vector> 4 5using namespace std; 6struct A 7{ 8 vector<future<int>> async_result; 9}; 10 11int main() 12{ 13 A* p = new A; 14 delete p; 15 return 0; 16}

の様に、構造体を動的確保しております。
テストプログラムをこの様に変更した所、上記同様のエラーが発生します。
エラー文からインスタンス化あたりでおかしな事になっているのだろうと見当をつけた所、
それが当たりでした。
しかし、なぜこれがコンパイルエラーになるのか分かりません。
どなたか教えて頂けますでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

こんにちは。

ubuntu 15.10上のgcc 5.2.1で-std=c++11を付けてコンパイルできました。
(C++11でcatseyeさんが回答されたテンプレート引数終端の>>問題が対策されてます)
-std=c++11を付けない場合はcatseyeさんの言う通りの可能性もあります。

ただし、微妙にミスがありコンパイルできなかったので、元ソースとは異なるようです。(...以外の部分でも)
その相違点のどこかに不具合が潜んでいる可能性もあります。-std=c++11無し時のメッセージが異なりますので、こちらの可能性が疑わしいです。

C++

1#include <functional> 2#include <future> 3#include <vector> 4 5using namespace std; 6struct A 7{ 8 vector<future<int>> async_result; 9}; 10 11int main() 12{ 13 A a; 14 15 return 0; 16}

因みに、-std=c++11を指定しなかった場合、下記エラーになりました。

In file included from /usr/include/c++/5/future:35:0, from test.cpp:3: /usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. #error This file requires compiler and library support for the \ ^ test.cpp:9:12: error: ‘future’ was not declared in this scope vector<future<int>> async_result; ^ test.cpp:9:25: error: template argument 1 is invalid vector<future<int>> async_result; ^ test.cpp:9:25: error: template argument 2 is invalid

投稿2016/01/24 00:33

編集2016/01/24 00:36
Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2016/01/24 00:44

「テンプレート引数終端の>>問題が対策されてます」勉強になりました。ありがとう
mao999

2016/01/24 03:29

ご回答ありがとうございます。 一番単純なテストプログラムを書けば良かったのですね。 ご回答いただいた通りのテストを書いたところ、コンパイルが通りました。 元ソースの他の部分で微妙なミスがある様ですね。 元々動いていたソースに、C++11仕様の非同期処理を導入したところ、こうなってしまいました。 探ってみます。分かり次第、また書きます。 ありがとうございます。
guest

0

ベストアンサー

ソースコードには問題がないように見えます。

対象がRaspberry Pi/armv7アーキテクチャとのことですが、Bug 64735 - std::future broken on armelあたりが関係するのでは?

エラーメッセージ自体は、std::futureクラステンプレートの宣言はあるが、その定義が存在しないと言っています(厳密にはコンストラクタとデストラクタの定義がない)。本来、そのようなことはありえないため、C++標準ライブラリ側の問題のように見えます。

該当環境でATOMIC_INT_LOCK_FREEマクロの値を表示させてみて、0 or 1とでるようなら残念ながらその環境ではstd::futureは非対応ということになります。

cpp

1#include <atomic> 2#include <iostream> 3int main() { 4 std::cout << ATOMIC_INT_LOCK_FREE << std::endl; 5}

投稿2016/01/31 15:00

編集2016/02/01 03:50
yohhoy

総合スコア6191

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mao999

2016/02/09 14:48

ご回答ありがとうございます。 遅くなりました。 提示していただいたプログラムを実行させたところ、1が表示されました。 std::futureには非対応みたいですね。 ATOMIC_INT_LOCK_FREE > 1になるようにSTLをリビルド等出来れば良いのですが、 代案を探すことにします。 ありがとうございました。
guest

0

 #include <vector>は?
あと
vector<future<int>> async_result;

の間を空けましょう。

vector<future<int> > async_result;
・・・違ったらごめん^^;
参考
C++で簡単非同期処理

投稿2016/01/23 23:39

編集2016/01/24 00:04
cateye

総合スコア6851

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mao999

2016/01/24 03:20

ご回答ありがとうございます。 vectorはincludeしてあります。 >>問題も疑ったのですが、違ったみたいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問