2016-11-08 7 views
-2

ベクトルのpush_back関数を使用しているこのコードセグメントをコンパイルすると、エラーが発生します。コンパイル時のベクトルpush_backエラー

for (int i=0; i<=n; i++) 
{ 
    if(i==0) 
    { 
     Profit[i].push_back(0); 
     Weight[i].push_back(0); 
     Name[i].push_back(""); 
    } 
    else 
    { 
     Profit[i].push_back(tosteal[i-1].getProfit()); 
     Weight[i].push_back(tosteal[i-1].getWeight()); 
     Name[i].push_back(tosteal[i-1].getName()); 
    } 
} 

重みと利益は、intデータ型のベクトルとして宣言され、Nameは文字列データ型のベクトルです。 tostealはアイテムオブジェクトの配列です。 getProfit()とgetWeight()はintを返し、getName()は文字列を返します。

joulethiefdynamicrefined.cpp:167:エラー:「Profit.std ::ベクトル< _Tp、_Alloc>のメンバーのリクエスト '一back' ::演算子

これらは、いくつかは繰り返していると、コンパイラが与えるエラーです[クラスの型がintではありません] [] [_Tp = int、_Alloc = std :: allocator((long unsigned int)i)) '、非クラス型のint型です joulethiefdynamicrefined.cpp:168:error: weight.std :: vector内のpush_back 'は、Weight.std :: vectorの中では無制限であるため、Weight.std :: vectorの'クラス型 'int' joulethiefdynamicrefined.cpp:169:エラー: 'const char *'から 'char'への無効な変換{_CharT = char、_Traits = std :: char_traits、_Alloc = std :: _CharT、_Traits、_Alloc :: _CharT)を使用して、 [アトリビュート] ' joulethiefdynamicrefined.cpp:173:エラー:Profit.std :: vectorのメンバー' push_back 'のリクエスト< _Tp、_Alloc> ::演算子[] _Tp = int、_Alloc = std :: allocator]( 174:エラー: 'Weight.std :: vector'内のメンバ 'push_back'のリクエスト< _Tp、_Alloc>:エラーが発生しました。 :演算子[] [_Tp = int、_Alloc = std :: allocator((long unsigned int)i)) '、非クラス型' int ' joulethiefdynamicrefined.cpp:175:エラー:一致しません'std :: basic_string、std :: allocator> :: push_back(std :: st)の呼び出しのための関数リング) ' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:914:note :候補である:ボイドのstd :: < _CharT、_Traits、_Alloc> ::一back(_CharT)と_CharT =チャー、_Traits = STD :: char_traits、_Alloc = STD ::アロケータ]

+1

あなたが求めるものではなく、for(int i = 0; i <= n; i ++)の 'i'の範囲は正しく感じません。 –

+0

_WeightとProfitはintデータ型のベクトルとして宣言されています_... okなぜWeight [i] .push_back'を行う必要がありますか? – smac89

答えて

5
Profit[i].push_back(0); 

をすべきのbasic_string be

Profit.push_back(0); 

などです。 Profitはベクターそのものです。 Profit[i].push_back(0)と言って、何かをベクトルにプッシュするのではなく、すでにベクトルに入っている要素の1つに何かを押し込もうとしています。

要素型がintあるので、Profit[i]は、あなたがエラーを取得する理由である、タイプintである:request for member ‘push_back’ in [...] which is of non-class type ‘int’

関連する問題