c++ - vector<string>::push_back(stringstream&) 没有匹配函数

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

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

最佳答案

您正在将 stringstream 插入 string 的 vector 中。变化

P.push_back(s); 

P.push_back(s.str()); 

关于c++ - vector<string>::push_back(stringstream&) 没有匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627176/

相关文章:

c++ - 如何在C++中表示无理数

c++ - 我需要在此代码中调用 SafeArrayUnLock 吗?

c++ - 使用VS2015为Vista编译C++

c++ - GCC 将私有(private)继承转换为父级

Java 字符串拆分函数表现奇怪

c++ - 有没有办法在 C++ 中将方法存储在某种类型的 vector 中?

c - 如何使用位操作将 char 数组 [example 42] 转换为 int equivilant [int 42]?

c# - 如何将整数日期转换为格式化日期字符串(即 2012009 到 2/01/2009)

c++ - 将每行两个单词的文件读取到 2 个不同的 vector 中

vector 的 C++ STL vector 和内存管理