編譯涉及使用向量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數據類型的載體和名稱是字符串數據類型的載體。 tosteal是項目對象的數組。 getProfit()和getWeight()返回一個int,getName()返回一個字符串。
這些是編譯器爲錯誤的,有些是重複:
joulethiefdynamicrefined.cpp:167:錯誤:請求部件「Profit.std '的push_back' ::矢量< _TP,_Alloc> ::操作者[] [with _Tp = int,_Alloc = std :: allocator](((long unsigned int)i))',它是非類類型'int' joulethiefdynamicrefined.cpp:168:error:request for member' ('(long unsigned int)i))'中的'push_back',其中'weight_std :: vector'中的'push_back'類型'int' joulethiefdynamicrefined.cpp:169:錯誤:從'const char *'無效轉換爲'char' joulethiefdynamicrefined.cpp:169:錯誤:初始化參數1'void std :: basic_string < _CharT,_Traits,_Alloc> :: push_back(_CharT)[with _CharT = char,_Traits = std :: char_traits,_Alloc = std ::分配器]' joulethiefdynamicrefined.cpp:173:錯誤:在'Profit.std :: vector < _Tp,_Alloc> :: operator [] [with _Tp = int,_Alloc = std :: allocator]中的成員'push_back'的請求( ((long unsigned int)i))',它是非類類型'int' joulethiefdynamicrefined.cpp:174:錯誤:請求成員'push_back'在'Weight.std :: vector < _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 :: basic_string的< _CharT,_Traits,_Alloc> ::的push_back(_CharT)[與_CharT =炭,_Traits =標準:: char_traits,_Alloc =標準::分配器]
不是你要求的,但'for(int i = 0; i <= n; i ++)'中的'i'的範圍並不合適。 –
_Weight和Profit是int數據類型_...的已聲明向量,那麼爲什麼您需要執行'Weight [i] .push_back'? – smac89