GoldenLee
GoldenLee

Reputation: 747

stringstream conversion error

I had ever used the following code snippet in one class. Today, I copied it to another class. When compiling, I got the following errors. It is strange that such a thing has actually happened! Would you please tell what's the matter? Thank you.

I've just been using the following snippet code to do a test in a demo project. I ran like a charm!

int main()
{  
   char buffer[] = "I1 I2 V1 V2 I3 V3 I4 DO V4";

   std::stringstream s(buffer); 
   std::istream_iterator<std::string> begin(s); 
   std::istream_iterator<std::string> end; 
   std::vector<std::string> IVector;
   std::vector<std::string> VVector;

   for ( ; begin != end; ++begin) 
   {     
       std::string sElem = *begin;

       switch((*begin)[0])
       {  
          case 'I':
            IVector.push_back( sElem);
            break;
          case 'V':
            VVector.push_back( sElem);
            break;
          default:
           ;
       }
   }

   return 0;
}

void ClassifyChannel(char* szBuffer)
{   
    // Empty vectors
    m_svIRChannels.clear();
    m_svVISChannels.clear();

    std::stringstream s(szBuffer); 
    std::istream_iterator<std::string> itBegin(s); 
    std::istream_iterator<std::string> itEnd; 

    for (; itBegin != itEnd; ++itBegin) 
    {     
        std::string sElem = *itBegin;

        // Switch on first character
        switch ((*itBegin)[0])
        {  
           // Infrared channel, such as IR1, IR2, IR3 (WV), and IR4
           case 'I':
           case 'W':
              // Insert into IR channel vector here 
              m_svIRChannels.push_back(sElem);
              break;
           // Visible channels, such as VIS, and VIS1KM
           case 'V':
              // Insert into VIS channel vector here 
              m_svVISChannels.push_back(sElem);
              break;
        }
    } 
} 

The error message is

error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>f:\tips\tips\fy2hdf5dataiohandler.cpp(830) : error C2664: 'std::istream_iterator<_Ty>::istream_iterator(std::basic_istream<_Elem,_Traits> &)' : cannot convert parameter 1 from 'int' to 'std::basic_istream<_Elem,_Traits> &'
1>        with
1>        [
1>            _Ty=std::string,
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        and
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]    

Upvotes: 1

Views: 2995

Answers (3)

user1833852
user1833852

Reputation: 51

#include <sstream> 

is required to include.

I tried in VS2017 and VS2019 and it is working by including this header.

Following is the sample code I tried.

int main()
{
    string line = "asasas asasas asasas asasas must try";
    stringstream check1(line);
}

Upvotes: 0

t.g.
t.g.

Reputation: 1749

What compiler are you using? Your code snippet compiles fine with VC10 on my box. Did you forget to include all the proper header files? For this piece of code you need these 3 headers:

#include <string>
#include <sstream>
#include <iterator>

Since VC10, <iterator> is not implicitly included by others. You need to include it yourself.

Upvotes: 3

Frerich Raabe
Frerich Raabe

Reputation: 94299

My guess; you didn't explicitely do an

#include <string>

The IO stream headers just have a forward-declaration of std::string, but this is not sufficient in your case. Try explicitely including the string header to resolve this.

Upvotes: 0

Related Questions