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

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

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

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

Q&A

解決済

1回答

925閲覧

C++STLのarrayの型にpair型を入れるとコンパイルエラーが発生する

moosan

総合スコア2

C++

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

0グッド

0クリップ

投稿2022/09/23 10:41

前提

C++STLのarrayとpairの文法についての質問です.

実現したいこと

arrayの型にpair型を与えるとコンパイルエラーが起きる理由について知りたいです.

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

Main.cpp:6:10: error: no viable conversion from 'int' to 'std::pair<int, std::array<int, 2> >' {1, {1, 2}}, ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:303:17: note: candidate constructor not viable: no known conversion from 'int' to 'const std::pair<int, std::array<int, 2> > &' for 1st argument constexpr pair(const pair&) = default; ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:304:17: note: candidate constructor not viable: no known conversion from 'int' to 'std::pair<int, std::array<int, 2> > &&' for 1st argument constexpr pair(pair&&) = default; ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:291:19: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'int' constexpr pair(const pair<_U1, _U2>& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:360:12: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'int' constexpr pair(pair<_U1, _U2>&& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:300:21: note: explicit constructor is not a candidate explicit constexpr pair(const pair<_U1, _U2>& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:370:21: note: explicit constructor is not a candidate explicit constexpr pair(pair<_U1, _U2>&& __p) ^ Main.cpp:6:13: error: no matching constructor for initialization of 'std::pair<int, std::array<int, 2> >' {1, {1, 2}}, ^~~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:260:17: note: candidate constructor template not viable: no known conversion from 'int' to 'const std::array<int, 2>' for 2nd argument constexpr pair(const _T1& __a, const _T2& __b) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:311:18: note: candidate constructor template not viable: no known conversion from 'int' to 'const std::array<int, 2>' for 2nd argument constexpr pair(_U1&& __x, const _T2& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:269:26: note: candidate template ignored: requirement '!_PCC<true, int, std::array<int, 2> >::_ImplicitlyConvertiblePair()' was not satisfied [with _U1 = int, _U2 = std::array<int, 2>] explicit constexpr pair(const _T1& __a, const _T2& __b) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:318:27: note: candidate template ignored: requirement '_PCC<true, int, std::array<int, 2> >::_MoveCopyPair()' was not satisfied [with _U1 = int] explicit constexpr pair(_U1&& __x, const _T2& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:325:18: note: candidate template ignored: requirement '_PCC<true, int, std::array<int, 2> >::_CopyMovePair()' was not satisfied [with _U2 = int] constexpr pair(const _T1& __x, _U2&& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:332:17: note: candidate template ignored: requirement '_PCC<true, int, std::array<int, 2> >::_CopyMovePair()' was not satisfied [with _U2 = int] explicit pair(const _T1& __x, _U2&& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:341:12: note: candidate template ignored: requirement '_PCC<true, int, std::array<int, 2> >::_MoveConstructiblePair()' was not satisfied [with _U1 = int, _U2 = int] constexpr pair(_U1&& __x, _U2&& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:350:21: note: candidate template ignored: requirement '_PCC<true, int, std::array<int, 2> >::_MoveConstructiblePair()' was not satisfied [with _U1 = int, _U2 = int] explicit constexpr pair(_U1&& __x, _U2&& __y) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:291:19: note: candidate constructor template not viable: requires single argument '__p', but 2 arguments were provided constexpr pair(const pair<_U1, _U2>& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:300:21: note: candidate constructor template not viable: requires single argument '__p', but 2 arguments were provided explicit constexpr pair(const pair<_U1, _U2>& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:303:17: note: candidate constructor not viable: requires 1 argument, but 2 were provided constexpr pair(const pair&) = default; ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:304:17: note: candidate constructor not viable: requires 1 argument, but 2 were provided constexpr pair(pair&&) = default; ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:360:12: note: candidate constructor template not viable: requires single argument '__p', but 2 arguments were provided constexpr pair(pair<_U1, _U2>&& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:370:21: note: candidate constructor template not viable: requires single argument '__p', but 2 arguments were provided explicit constexpr pair(pair<_U1, _U2>&& __p) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:375:9: note: candidate constructor template not viable: requires 3 arguments, but 2 were provided pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:229:26: note: candidate constructor template not viable: requires 0 arguments, but 2 were provided _GLIBCXX_CONSTEXPR pair() ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:242:26: note: candidate constructor template not viable: requires 0 arguments, but 2 were provided explicit constexpr pair() ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:436:9: note: candidate constructor template not viable: requires 4 arguments, but 2 were provided pair(tuple<_Args1...>&, tuple<_Args2...>&, ^ 2 errors generated.

該当のソースコード

c++

1#include <bits/stdc++.h> 2using namespace std; 3int main(void){ 4 // Your code here! 5 array<pair<int, array<int, 2>>, 2> { 6 {1, {1, 2}}, 7 {2, {1, 2}} 8 }; 9 return 0; 10}

試したこと

vectorに直すとコンパイルが通るようになります.

c++

1#include <bits/stdc++.h> 2using namespace std; 3int main(void){ 4 // Your code here! 5 vector<pair<int, array<int, 2>>> { 6 {1, {1, 2}}, 7 {2, {1, 2}} 8 }; 9 return 0; 10}

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

C17++ / clang version 10.0.0-4ubuntu1
https://paiza.io/projects/oufhnDISoQkzfmbs9JLyng?language=cpp

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

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

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

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

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

guest

回答1

0

ベストアンサー

std::array は集成体として扱われるルールなので、波括弧の中をコンストラクタに渡す std::vector とは解釈が異なります。 中括弧が余計にひとつ必要です。

cpp

1array<pair<int, array<int, 2>>, 2> {{ 2 {1, {1, 2}}, 3 {2, {1, 2}} 4}};

投稿2022/09/23 11:16

SaitoAtsushi

総合スコア5544

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

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

moosan

2022/09/23 11:46

理解・解決致しました. 回答ありがとうございます.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.41%

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

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

質問する

関連した質問