vector <string> :: push_back(stringstream&amp;)</string>没有匹配函数

时间:2014-03-25 06:43:45

标签: c++ string vector stl

所以我正在为TopCoder问题编写算法,我收到了以下错误。我尽力消除语法错误,但我似乎无法弄清楚这些错误的原因。你能帮帮我吗?我不是很有经验,我正在努力学习。

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;
       }
     }

如果有人能指出我做错了什么,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

您正在将stringstream推送到string s的向量中。变化

P.push_back(s); 

P.push_back(s.str());