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

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

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

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

参照

参照は、プログラミングにおいて変数や関数といったメモリ空間上での所在を指示するデータのことを指します。その中にはデータ自体は含まれず、他の場所にある情報を間接的に指示するプログラムです。

STL

STL(Standard Template Library)は、ジェネティックコンテイナー、イテレーター、アルゴリズム、そして関数オブジェクトのC++ライブラリーです。

C++

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

Q&A

解決済

1回答

490閲覧

gccで参照オブジェクトに対して操作したい

s8079

総合スコア36

C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

参照

参照は、プログラミングにおいて変数や関数といったメモリ空間上での所在を指示するデータのことを指します。その中にはデータ自体は含まれず、他の場所にある情報を間接的に指示するプログラムです。

STL

STL(Standard Template Library)は、ジェネティックコンテイナー、イテレーター、アルゴリズム、そして関数オブジェクトのC++ライブラリーです。

C++

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

0グッド

1クリップ

投稿2020/02/19 15:01

前提・実現したいこと

reference_wrapperによる参照オブジェクトに対して値を操作したいです.
私が書いたコードはclangではコンパイル可能ですが,gccではコンパイルできません.
そこで,どのように書き換えれば良いのか教えていただきたく質問しました.
よろしくお願いします.

該当のソースコード

C++

1#include <iostream> 2#include <vector> 3std::vector<std::reference_wrapper<int>> func(std::vector<int> *vec, const std::vector<int> &idx) { 4 std::vector<std::reference_wrapper<int>> ret; 5 for (int i = 0; i < idx.size(); ++i) { 6 ret.emplace_back(vec->at(idx.at(i))); 7 } 8 return ret; 9} 10int main() { 11 std::vector<int> foo = { 0, 1, 2, 3, 4, 5 }; 12 std::vector<int> idx = { 0, 2, 5 }; 13 std::vector<std::reference_wrapper<int>> bar = func(&foo, idx); 14 for (int i = 0; i < bar.size(); ++i) { 15 bar.at(i) += 10; 16 } 17 for (int i = 0; i < foo.size(); ++i) { 18 std::cout << foo.at(i) << " "; 19 } 20 std::cout << std::endl; 21 return 0; 22}

発生している問題・エラーメッセージ

prog.cc:15:13: error: no match for 'operator+=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::reference_wrapper<int> > >::value_type {aka std::reference_wrapper<int>}' and 'int') bar.at(i) += 10; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/bits/vector.tcc:98:6: error: cannot increment a pointer to incomplete type 'std::reference_wrapper<int>' ++this->_M_impl._M_finish; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/type_traits:1922:11: error: declaration of 'class std::reference_wrapper<int>' class reference_wrapper; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/bits/stl_vector.h:161:9: error: invalid use of incomplete type 'class std::reference_wrapper<int>' - this->_M_impl._M_start); } ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/type_traits:1922:11: error: declaration of 'class std::reference_wrapper<int>' class reference_wrapper; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/bits/stl_vector.h:780:41: error: invalid use of incomplete type 'class std::reference_wrapper<int>' { return *(this->_M_impl._M_start + __n); } ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/type_traits:1922:11: error: declaration of 'class std::reference_wrapper<int>' class reference_wrapper; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/bits/stl_construct.h:127:27: error: invalid use of incomplete type '_Value_type {aka class std::reference_wrapper<int>}' __destroy(__first, __last); ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/type_traits:1922:11: error: declaration of '_Value_type {aka class std::reference_wrapper<int>}' class reference_wrapper; ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/ext/new_allocator.h:120:4: error: invalid use of incomplete type 'class std::reference_wrapper<int>' { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^ /opt/wandbox/gcc-4.9.2/include/c++/4.9.2/type_traits:1922:11: error: declaration of 'class std::reference_wrapper<int>' class reference_wrapper; ^

補足情報(FW/ツールのバージョンなど)

C++11

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

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

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

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

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

guest

回答1

0

ベストアンサー

cpp

1#include <functional>

を追加してください。

投稿2020/02/19 15:46

yumetodo

総合スコア5850

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問