1
所以我正在寫一個TopCoder問題的算法,我得到了以下錯誤。我盡我所能消除語法錯誤,但我似乎無法弄清楚是什麼保證了這些錯誤。你能幫我解決嗎?我不是很有經驗,我正在努力學習。沒有匹配函數向量<string> :: push_back(stringstream&)
tc1.cpp: In member function ‘std::vector<std::basic_string<char> > BinaryCode::decode(std::string)’:
tc1.cpp:46:20: error: no matching function for call to ‘std::vector<std::basic_string<char> >::push_back(std::stringstream&)’
tc1.cpp:46:20: note: candidate is:
/usr/include/c++/4.6/bits/stl_vector.h:826:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>]
/usr/include/c++/4.6/bits/stl_vector.h:826:7: note: no known conversion for argument 1 from ‘std::stringstream {aka std::basic_stringstream<char>}’ to ‘const value_type& {aka const std::basic_string<char>&}’
make: *** [tc1] Error 1
我寫的代碼 -
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
class BinaryCode
{
public:
vector<string> decode(string message)
{
int cntrl = 0;
vector<string> P;
bool poss = true;
int a = message.length();
int A[a+2], B[a+2];
for (int i=1; i<=a ; i++)
{
stringstream ss(message.substr(i,1));
ss >> B[i];
}
while (cntrl <2)
{
A[0] = A[a+1] = cntrl;
for (int i=0; i<=a; i++)
{
A[1] = B[2] - cntrl;
A[i+1] = B[i] - A[i] - A[i-1];
if (A[i+1]<0 || A[i+1])
{
poss = false;
}
}
if (!poss)
P.push_back("NONE");
else
{
stringstream s;
for (int i =0; i<a+2; i++)
s << (char)A[i];
P.push_back(s);
}
cntrl++;
}
return P;
}
};
int main()
{
BinaryCode ob;
string str;
cout << "Enter the encrypted string: " << endl;
cin >> str;
vector<string> vec = ob.BinaryCode::decode(str);
vector<string>::iterator it = vec.begin();
for (; it!= vec.end(); it++) {
cout << *it;
}
}
我會很感激,如果有人能指出我在做什麼錯。
請改變你的標題更具描述。 – Maroun
錯誤消息告訴您,您嘗試將字符串流添加到矢量。現在你需要的是一種將stringstream轉換爲字符串的方法(這裏有一個成員函數) –
你試圖爲int A [a + 2],B [a + 2 ]',請嘗試使用矢量。 –