C++のvectorについて教えてください

このQ&Aのポイント
  • C++のvectorについての疑問を解決するためにテストコードを書いたが、Debug版でコンパイルする際にエラーが出る
  • Release版でのコンパイルは通るため、Debug版で問題を解決する方法を知りたい
  • Visual Studio 2008 Academic Editionを使用している
回答を見る
  • ベストアンサー

C++のvectorについて教えてください。

C++のvectorについて教えてください。 現在悩んでいる問題について簡単に説明するために、テストコードを書きました。 #include <vector> class IntType { private: int num; public: IntType( int n ):num( n ){}; }; std::vector< IntType > IntVector; void main() { } このコードをDebug版でコンパイルすると 1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(285) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'const size_t' から減少できませんでした 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2236) : 'std::operator <' の宣言を確認してください。 のようなエラーが出ます。 しかし、Release版でコンパイルするとコンパイルは通ります。 Debug版でコンパイルを通すにはどのようなコードを追加すればよいでしょうか? 知恵を貸してください。 よろしくおねがいします。 /** VisualStudio2008 AcademiEdition */

  • 0xEF
  • お礼率59% (193/327)

質問者が選んだベストアンサー

  • ベストアンサー
  • qwertfk
  • ベストアンサー率67% (55/81)
回答No.1

vectorに指定するクラスにはデフォルトコンストラクタが必要です。 class IntType { private:  int num; public:  IntType(void):num(0) {};  IntType( int n ):num( n ){}; }; とすると、おそらくコンパイルできるようになるはず。 多分そのコードだとReleaseでも通らないと思うんですけどね...

0xEF
質問者

お礼

ありがとうございます。 VisualStudioを再インストールしたらコンパイルが通りました・・・ お騒がせしました><!

その他の回答 (2)

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.3

main の返り値を void にするのはいかがなものかと思うな. あと, この形ではデフォルトコンストラクタはいらないんじゃないかな>#1. 確かに一部の関数ではデフォルト引数としてデフォルトコンストラクタ (が作る値) を要求するけど, ここではそのような関数は使われていないので. とはいえコンテナライブラリではコピーコンストラクタと代入演算子を要求するので, やっぱり駄目であることに変わりはない.

  • hidebun
  • ベストアンサー率50% (92/181)
回答No.2

他のところが影響しているのではないでしょうか。 拡張子が.cになっているとか。 VisualStudio2008 Express editionでは何の変更も加えずにDebug/Releaseともコンパイルが通ります。

関連するQ&A

  • C++ vectorのbeginについて

    VC++2010にて下記コードのビルドは通るのですが、 vVecotrInt, vVectorIntPtrの要素数0のとき、iptr代入時に Debug assertaion Failed! vector iterator not dereferencableとなります。 そもそもbegin()はイテレータなので、ポインタに代入しようとしていることが間違いかと思うのですが。 質問1.int* iptr = ~ ではなく、std:vector<int>::iteratorとすれば、     要素数が0でもエラーがおきません。この違いは何でしょうか? 質問2.そもそもイテレータをポインタに代入して何か得することがあるんでしょうか?     ただイテレータとポインタは同じようなものだと思って、コーディングしてるだけなんでしょうか・・・ コード: // vVectorIntの要素を間接参照して(参照先はint)、そのアドレスをポインタに格納 std::vector<int> vVectorInt; int* iptr = &*vVectorInt.begin(); // ここでvector iterator not dereferencable // vVectorIntPtrの要素を間接参照して(参照先はint*)、ポインタに格納 std::vector<int*> vVectorIntPtr; int* iptr = *vVectorIntPtr.begin(); // ここでvector iterator not dereferencable // vVecotrIntPtr2の要素数0のときでも、イテレータを使えば問題ない std::vector<int*> vVectorIntPtr2; std::vector<int*>::iterator itr = vVectorIntPtr2.begin();

  • マップとアルゴリズム

    またまた質問です。 以下の処理をさせるとエラーが出ます。 どなたか見ていただけますか? 環境はWindows XP, Visual Studio.NETです。 -----処理部分------------------ map<char, int> m; for(i=0; i<10; i++){  m.insert(pair<char, int>('A'+i, i)); } reverse(m.begin(), m.end()); map<char, int>::iterator p; p = m.begin(); while( p != m.end() ){  cout << p->first << " " << p->second << endl;  p++; } ------以下エラーログ--------------- C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。 C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(46) : コンパイルされたクラスのテンプレートのインスタンス化 'void std::swap<const _Ty1>(const _Ty &,const _Ty &)' の参照を確認してください with [ _Ty1=int, _Ty=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(45): クラス テンプレートのメンバ関数 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2>::_Myt &)' のコンパイル中 with [ _Ty1=const int, _Ty2=int ] ClusteringMain.cpp(45) : コンパイルされたクラスのテンプレートのインスタンス化 'std::pair<_Ty1,_Ty2>' の参照を確認してください with [ _Ty1=const int, _Ty2=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。

  • 構造体?

    以下は、あるC++のサンプルプログラムの頭の部分なのですが、構造体associateの中に、再びassociateが2回も出てきます。何をしている記述なのか教えてください。 #include <vector> #include <algorithm> #include <functional> #include <iostream> using namespace std; struct associate { int num; char chr; associate(int n, char c) : num(n), chr(c) {};←この業は何? associate() : num(0), chr('\0'){};←この行は何? }; bool operator<(const associate &x, const associate &y) { return x.num < y.num; } ostream& operator<<(ostream &s, const associate &x) { return s << "<" << x.num << ";" << x.chr << ">"; } int main () { vector<associate>::iterator i, j, k; associate arr[20] = {associate(-4, ' '), associate(16, ' '), ・ ・ ・ associate(-3, 'e'), associate(15, ' ')};

  • C++ iteator const を使ったプログラムがコンパイルできない

    以下のプログラムがコンパイルできません。 理由もよく分かりません。 ----------------- #include <algorithm> #include <list> struct Hoge { std::list<Hoge*> l; Hoge * f() const { std::list<Hoge*>::iterator i = l.begin(); return 0; } }; -------------------- g++ 4.1.1 を使っていますが % g++ -c test1.cc test1.cc: In member function 'Hoge* Hoge::f() const': test1.cc:8: error: conversion from 'std::_List_const_iterator<Hoge*>' to non-scalar type 'std::_List_iterator<Hoge*>' requested となります。 l.begin() が const で代入できないということだと思うのですが std::list<Hoge*> l; の部分には const を使っていませんし良く分かりません。 また、 Hoge * eval() const { の const を消すか、 std::list<Hoge*>::iterator i = l.begin(); の代入をやめると コンパイルが通ります。 const は return するポインタの先の領域を変更不可にしている と解釈しているのですが、戻り値と関係ない代入がどうして影響を受けるのかが謎です。 (実際は、もう少し違う大きいプログラムですが関係なさそうな部分を削って 上でも同様のエラーメッセージが出ることを確認しました。)

  • Vectorのポインタが入ったvector

    windows Vista sp1, Visual C++ 2008でC++の勉強をしています。 Vectorへのポインタが入ったvectorを使うプログラムを書いているのですがうまくいかず、困っています。 どういうプログラムかというと、 入力ファイルの">"という記号を区切りとして、その間にある各行をひとまとめのグループとしてvectorにいれます。 さらに各vectorのポインタをべつのvectorに入れます。 最終的に区切りの数だけvectorができ、入力ファイルを読み終わった後に すべてのvectorを"各グループのポインタが入ったvector"からループ処理ですべて出力する、というものです。 /入力ファイル input.txt/ > human cat dog > beetle dragonfly spider > salmon saury catfish > vector1には human cat dog vector2にはbeetle dragonfly spider vector3にはsalmon saury catfish が入り、 別のvectorにそれぞれのvectorのポインタをいれ、 最後にこのvectorをつかって全ファイル内容を出力するというものです。 具体的に書くと、 ">"の区切りごとの各行のstringを入れるvectorとしてeach_vector。 each_vectorのポインタを入れるvectorをvector_of_ptr_each_vectorとします。 ">"を認識するごとに new で each_vectorの領域を確保し、そのポインタをvector_of_ptr_each_vectorに追加していき、">"のない行のstringを each_vectorに入れます。 ファイルの読み込みが終わった後でvector_of_ptr_each_vectorからイテレータを使って各vector(each_vector)の全要素をそれぞれ出力する、というものです。 以下のようにコードを書きました。 #include <fstream> #include <string> #include <vector> #include <iostream> using namespace std; int main( ) { ifstream ifs("input.txt"); string buf; std::vector<string> each_vector; std::vector<std::vector<string> *> vector_of_ptr_each_vector; while(ifs && getline(ifs, buf)) { if(buf[0] == '>'){ std::vector<string>* ptr_eachvector ; ptr_eachvector = new std::vector<string>; each_vector = *ptr_eachvector ; vector_of_ptr_each_vector.push_back(ptr_eachvector) ; } each_vector.push_back(buf) ; } printf("\n output from vector of ptr of vector\n"); std::vector<std::vector<string> *>::iterator it_b = vector_of_ptr_each_vector.begin(); while( it_b != vector_of_ptr_each_vector.end() ) { std::vector<string>::iterator it_c = it_b->begin();    //エラー1 while( it_c != it_b->end() ) //エラー2 { cout << *it_c << endl; ++it_c; } ++it_b; } return 0; } ですが、エラーでビルドされず、 std::vector<string>::iterator it_c = it_b->begin(); の行に関して error C2839: invalid return type 'std::vector<_Ty> **' for overloaded 'operator ->' error C2039: 'begin' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>' while( it_c != it_b->end() )   の行に関して error C2839: invalid return type 'std::vector<_Ty> **' for overloaded 'operator ->' 1> with 1> [ 1> _Ty=std::string 1> ] error C2039: 'end' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>' 1> with 1> [ 1> _Ty=std::vector<std::string> , 1> _Alloc=std::allocator<std::vector<std::string> > 1> ] fatal error C1903: unable to recover from previous error(s); stopping compilation というようなエラーが出ます。 vectorのポインタを入れたvectorの扱い、特にイテレータに関して問題があると思うのですが原因が分かりません。 また、new でのeach_vectorの領域確保の方法も怪しいという感じがします。 解決策、アドバイスありましたらよろしくお願いします。

  • const参照メンバを含む要素のvector

    久しぶりに趣味でプログラミングしてます。 const参照のメンバを含むオブジェクトを要素に持つvector って使えます? 下記はbcc32でコンパイルするとエラーが出ます。 _algobase.h 145:コンパイラは 'operator=' クラスの hoge を作成できない(関数 _STL::hoge * __copy<hoge *,hoge *,int>(hoge *,hoge *,hoge *,const random_access_iterator_tag &,int *) ) ちなみにv.clear();をコメントアウトするとコンパイルは通り実行も問題なくできます。 clear()の替わりにpop_back()としてもコンパイルは通り実行も問題なくできます。 コンパイラーのバグ?そもそもこんなことをしてはダメ? よろしくお願いします。 class hoge { public:  hoge(const int &i):_i(i){};  virtual ~hoge(){};  operator int () const {return _i;}  const int &_i; }; int main() {  int i1=1;  int i2=2;  hoge h1(i1);  hoge h2(i2);  std::vector<hoge > v;  v.push_back(h1);  v.push_back(h2);  v.clear();  std::vector<hoge >::iterator iter = v.begin();  while( iter != v.end() ){   std::cout << *iter << "\n";   ++iter;  }  return 0; }

  • basic_ostreamクラスについて

    http://msdn.microsoft.com/ja-jp/library/k262esc6.aspx をみると、VC++2010のbasic_ostream::sentryには  operator bool( ) const; がある事になっていますが、実際は無いようです。 VC++2010 Express Edition のostream(C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream)を見ると、sentryにoperator bool() constがありませんでした。 代わりに __CLR_OR_THIS_CALL _OPERATOR_BOOL() const { // test if stream state okay return (_Ok ? _CONVERTIBLE_TO_TRUE : 0); } というのがありますが、 #define _STD ::std:: #define _OPERATOR_BOOL operator _STD _Bool_type と定義されており、operator bool( ) constとは別物でした。 このため、basic_ostream::sentry::operator bool( )を使用しているソースをVC++2010(Express Edition)でビルドしようとするとエラーになります。 ちなみに、このソースをVC++2008 Express Editionでビルドすると通ります。 質問なのですが、 1)basic_ostream::sentry::operator bool( )を使うソースをVC++2010 Express Editionでビルドするにはどうすれば良いでしょうか? 2)なぜVC++2010 Express Editionからbasic_ostream::sentry::operator bool( )がなくなってしまったのでしょうか? ご存知の方、ご教授ください。 よろしくお願いします。

  • VC++のバグ?

    次のコードがコンパイルエラーとなってしまいます。 #include<vector> class foo:public std::vector<int>{ public: void clear(){ std::vector<int>::clear(); } }; VC6のSP2ではエラーになるんですが Linuxのgccでは全く問題なく動いたんです VC++のエラー?gccの拡張機能? .NETでは動くとか? どうなんでしょう? まあ、 using std::vector; や typedef std::vector<int> bar; とかすると回避可能なんですが…

  • VisualC++2005にてerror C2664と出ます。STLのpairで引数を変換できないとのことです

    私は現在、VisualC++2005 Express Editionにてプログラミングを行っているのですが、次のようなエラーが出てしまいます。 c:\program files\microsoft visual studio 8\vc\include\utility(44) : error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' : 1 番目の引数を 'const double' から 'const std::pair<_Ty1,_Ty2> &' に変換できません。(新しい機能 ; ヘルプを参照) with [ _Ty1=double, _Ty2=double ] 理由: 'const double' から 'const std::pair<_Ty1,_Ty2>' へは変換できません。 with [ _Ty1=double, _Ty2=double ] コンストラクタはソース型を持てません、またはコンストラクタのオーバーロードの解決があいまいです。 コードは次のものです。 typedef std::pair<double, double> doublesPair; typedef std::pair<doublesPair, double> posValDouble; typedef std::list<posValDouble> posValDoubleList; ---------------------------------------------------- posValDoubleList::iterator pvdl_ite = pvdl.begin(); Double ax = pvdl_ite->first.first; ay = pvdl_ite->first.second; az = pvdl_ite->second; pvdl_ite++; bx = --- - - - 解決策を教えていただけたらと思います。 よろしくお願いします。

  • C言語の配列をC++のvectorに高速に変換したい

    質問は表題のとおりです。 単純な方法では以下の通りになると思いますが、 (C言語文字列から string への変換のように) 一括変換の仕組みは vector にないのでしょうか? static const int n=5; int a[n]={0,1,2,3,4}; std::vector<int> v(n); std::vector<int>::iterator vit=v.begin(); for(int i=0; i<n; i++){ *vit++ = a[i]; } 上記の例では n=5 ですが、nがとても大きな場合に適用したいと考えています。 ちなみに、gcc 3.4.3 を使っています。

専門家に質問してみよう