STLがこれをコンパイルするために一時的なイテレータ変数を必要とするのはなぜですか?

jpo38

これは非常に単純なコードです。

#include <vector>

int main() {

    std::vector<int> myVec(5);
    std::vector<int>::const_iterator first = myVec.begin();
    std::vector<int>::const_iterator last = myVec.begin() + 3;
    std::vector<int> newVec1(first, last);
    std::vector<int> newVec2(myVec.begin(), last);

    return 0;
}

newVec1コンパイルを宣言する行

行宣言newVec2は次のエラーで失敗します:

prog.cpp: In function 'int main()':
prog.cpp:11:49: error: no matching function for call to 'std::vector<int>::vector(std::vector<int>::iterator, std::vector<int>::const_iterator&)'
     std::vector<int> newVec2(myVec.begin(), last);
                                                 ^
prog.cpp:11:49: note: candidates are:
In file included from /usr/include/c++/4.9/vector:64:0,
                 from prog.cpp:3:
/usr/include/c++/4.9/bits/stl_vector.h:401:9: note: template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)
         vector(_InputIterator __first, _InputIterator __last,
         ^
/usr/include/c++/4.9/bits/stl_vector.h:401:9: note:   template argument deduction/substitution failed:
prog.cpp:11:49: note:   deduced conflicting types for parameter '_InputIterator' ('__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and '__gnu_cxx::__normal_iterator<const int*, std::vector<int> >')
     std::vector<int> newVec2(myVec.begin(), last);
                                                 ^
In file included from /usr/include/c++/4.9/vector:64:0,
                 from prog.cpp:3:
/usr/include/c++/4.9/bits/stl_vector.h:373:7: note: std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(initializer_list<value_type> __l,
       ^
/usr/include/c++/4.9/bits/stl_vector.h:373:7: note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'std::initializer_list<int>'
/usr/include/c++/4.9/bits/stl_vector.h:348:7: note: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(vector&& __rv, const allocator_type& __m)
       ^
/usr/include/c++/4.9/bits/stl_vector.h:348:7: note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'std::vector<int>&&'
/usr/include/c++/4.9/bits/stl_vector.h:339:7: note: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(const vector& __x, const allocator_type& __a)
       ^
/usr/include/c++/4.9/bits/stl_vector.h:339:7: note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'const std::vector<int>&'
/usr/include/c++/4.9/bits/stl_vector.h:335:7: note: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>]
       vector(vector&& __x) noexcept
       ^
/usr/include/c++/4.9/bits/stl_vector.h:335:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/4.9/bits/stl_vector.h:318:7: note: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]
       vector(const vector& __x)
       ^
/usr/include/c++/4.9/bits/stl_vector.h:318:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/4.9/bits/stl_vector.h:289:7: note: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(size_type __n, const value_type& __value,
       ^
/usr/include/c++/4.9/bits/stl_vector.h:289:7: note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
/usr/include/c++/4.9/bits/stl_vector.h:277:7: note: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(size_type __n, const allocator_type& __a = allocator_type())
       ^
/usr/include/c++/4.9/bits/stl_vector.h:277:7: note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
/usr/include/c++/4.9/bits/stl_vector.h:264:7: note: std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]
       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
       ^
/usr/include/c++/4.9/bits/stl_vector.h:264:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/4.9/bits/stl_vector.h:253:7: note: std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]
       vector()
       ^
/usr/include/c++/4.9/bits/stl_vector.h:253:7: note:   candidate expects 0 arguments, 2 provided

g ++とVisualStudioの両方がこれをコンパイルできません。理由は何ですか?myVec.begin()と同じfirstです...

TartanLlama

myVec.begin()と同じではありませんfirstfirstタイプであるstd::vector<int>::const_iteratorのに対し、myVec.begin()タイプのものですstd::vector<int>::iterator

constイテレータが必要な場合は、次を使用しますcbegin

std::vector<int> newVec2(myVec.cbegin(), last);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

コードをコンパイルするためにこれらの正確なライフタイムが必要なのはなぜですか?

分類Dev

コードをコンパイルするためにclangでlibgcc.aが必要なのはなぜですか?

分類Dev

forループに移動したイテレータが不変であるとコンパイラが文句を言わないのはなぜですか?

分類Dev

RustをコンパイルするためにC ++コンパイラが必要なのはなぜですか?

分類Dev

コンパイル時に.oファイルを含める必要があるのはなぜですか?

分類Dev

サイズとして変数が与えられた文字配列を持つこのコードがコンパイルされるのはなぜですか?

分類Dev

ターミナルで共通の実行可能ファイルをビルドするために一度に2つのcファイルをコンパイルすることが機能しないのはなぜですか?

分類Dev

コンパイル時に不明な数のイテレータを一緒に圧縮するにはどうすればよいですか?

分類Dev

Kotlinで変更されていないコレクションを宣言するために、常に不変のコレクションインターフェイスを使用する必要があるのはなぜですか?

分類Dev

constexprを使用した変数の初期化が、コンパイル時ではなく実行時に評価されるのはなぜですか

分類Dev

Javaがコンパイル時に変数をバインドするのはなぜですか?

分類Dev

変数を1行にエコーするためにbashが&&を必要とするのはなぜですか?

分類Dev

変数が渡されるためにループ中にある必要がある場合、コンパイラが変数が初期化されない可能性があると言うのはなぜですか?

分類Dev

「std :: thread」ファンクターをコンパイルするために追加の括弧が必要なのはなぜですか?

分類Dev

可変個引数テンプレートパラメータで構造体を使用すると、2つのテンプレートが同時にインスタンス化されるのはなぜですか?

分類Dev

この関数をバインドするために「this」が必要なのはなぜですか?

分類Dev

Char変数をnilに設定すると、コンパイルが失敗するのはなぜですか?

分類Dev

コンパイラが関数シグネチャにこのような厳密な一致を必要とするのはなぜですか?

分類Dev

Android PieがrecyclerViewのアイテムの一部である編集テキストにフォーカスを維持するためにダブルタップを必要とするのはなぜですか?それを解決するには?

分類Dev

関数パラメーターにCのアドレスまたは変数を含めることができるのはなぜですか

分類Dev

gccを使用するプリコンパイル済みヘッダーを含める前に、gccの__COUNTER__マクロが展開されていないことを確認する必要があるのはなぜですか?

分類Dev

文字列を数値に解析するときに、「この値のタイプはこのコンテキストで認識されている必要があります」というエラーが表示されるのはなぜですか?

分類Dev

コンパイラがこの変数を間違った値に初期化するのはなぜですか?これは配置の問題ですか?

分類Dev

<=>をコンパイルするために<compare>ヘッダーを含める必要があるのはなぜですか?

分類Dev

コレクションを列挙するために2つのインターフェイスが必要なのはなぜですか?

分類Dev

これを実行するためにifステートメントが必要なのはなぜですか

分類Dev

事前にトレーニングされたモデルをKerasでコンパイルして適合させる必要があるのはなぜですか?

分類Dev

C ++ 17の `constexpr if`を使用したこのテンプレート構造体がMSVCでのコンパイルに失敗するのはなぜですか?

分類Dev

Windowsでファイルをコピーまたは移動するときにタイムスタンプが変更されるのはなぜですか?

Related 関連記事

  1. 1

    コードをコンパイルするためにこれらの正確なライフタイムが必要なのはなぜですか?

  2. 2

    コードをコンパイルするためにclangでlibgcc.aが必要なのはなぜですか?

  3. 3

    forループに移動したイテレータが不変であるとコンパイラが文句を言わないのはなぜですか?

  4. 4

    RustをコンパイルするためにC ++コンパイラが必要なのはなぜですか?

  5. 5

    コンパイル時に.oファイルを含める必要があるのはなぜですか?

  6. 6

    サイズとして変数が与えられた文字配列を持つこのコードがコンパイルされるのはなぜですか?

  7. 7

    ターミナルで共通の実行可能ファイルをビルドするために一度に2つのcファイルをコンパイルすることが機能しないのはなぜですか?

  8. 8

    コンパイル時に不明な数のイテレータを一緒に圧縮するにはどうすればよいですか?

  9. 9

    Kotlinで変更されていないコレクションを宣言するために、常に不変のコレクションインターフェイスを使用する必要があるのはなぜですか?

  10. 10

    constexprを使用した変数の初期化が、コンパイル時ではなく実行時に評価されるのはなぜですか

  11. 11

    Javaがコンパイル時に変数をバインドするのはなぜですか?

  12. 12

    変数を1行にエコーするためにbashが&&を必要とするのはなぜですか?

  13. 13

    変数が渡されるためにループ中にある必要がある場合、コンパイラが変数が初期化されない可能性があると言うのはなぜですか?

  14. 14

    「std :: thread」ファンクターをコンパイルするために追加の括弧が必要なのはなぜですか?

  15. 15

    可変個引数テンプレートパラメータで構造体を使用すると、2つのテンプレートが同時にインスタンス化されるのはなぜですか?

  16. 16

    この関数をバインドするために「this」が必要なのはなぜですか?

  17. 17

    Char変数をnilに設定すると、コンパイルが失敗するのはなぜですか?

  18. 18

    コンパイラが関数シグネチャにこのような厳密な一致を必要とするのはなぜですか?

  19. 19

    Android PieがrecyclerViewのアイテムの一部である編集テキストにフォーカスを維持するためにダブルタップを必要とするのはなぜですか?それを解決するには?

  20. 20

    関数パラメーターにCのアドレスまたは変数を含めることができるのはなぜですか

  21. 21

    gccを使用するプリコンパイル済みヘッダーを含める前に、gccの__COUNTER__マクロが展開されていないことを確認する必要があるのはなぜですか?

  22. 22

    文字列を数値に解析するときに、「この値のタイプはこのコンテキストで認識されている必要があります」というエラーが表示されるのはなぜですか?

  23. 23

    コンパイラがこの変数を間違った値に初期化するのはなぜですか?これは配置の問題ですか?

  24. 24

    <=>をコンパイルするために<compare>ヘッダーを含める必要があるのはなぜですか?

  25. 25

    コレクションを列挙するために2つのインターフェイスが必要なのはなぜですか?

  26. 26

    これを実行するためにifステートメントが必要なのはなぜですか

  27. 27

    事前にトレーニングされたモデルをKerasでコンパイルして適合させる必要があるのはなぜですか?

  28. 28

    C ++ 17の `constexpr if`を使用したこのテンプレート構造体がMSVCでのコンパイルに失敗するのはなぜですか?

  29. 29

    Windowsでファイルをコピーまたは移動するときにタイムスタンプが変更されるのはなぜですか?

ホットタグ

アーカイブ