How resolve problem of use of deleted function std basic_stringstream

I've written the following in windows on Qt IDE and when I run it, it works well,
however where I tried to run on centOS with

g++ -std=gnu++11 main.cpp -o main

I get these errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream> // for standard I/O
  #include <string>   // for strings
  #include <iomanip>  // for controlling float print precision
  #include <sstream>  // string to number conversion
  
  #include <ctime>
  #include <future>
  #include <fstream>
  #include <string>
  
  /*
  * 
  * error This file requires compiler and library support for the ISO C++ 2011 standard. 
  * This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. 
  *
  */
  
  using namespace std;
  
  stringstream processing (int x,int id) {
  
      std::cout << "Calculating. Please, wait...\n";
      stringstream cvsStream;
      for(int i = 0 ; i < x ; ++i){
          cvsStream <<i<<","<<i<<","<<i<<","<<i<<"\n";
          cout <<id<< " / "<<i<< endl;
      }
  return cvsStream;
  }
  
  int main(int argc, char *argv[])
  {
      string filename = "OutputFile.csv";
      ofstream myfile;
      stringstream cvsStream;
  
      myfile.open(filename);
      // If file does not exist, Create new file
      if (!myfile )
      {
          cout << "Cannot open file, file does not exist. Creating new file..";
          myfile.open(filename,  fstream::in | fstream::out | fstream::trunc);
          myfile <<"\n";
      }
      // open csv file
      cvsStream <<" AD_ID "<<","<<"Starts at "<<","<<"At_Frame"<<","<<"Ends at "<<"\n";
      myfile << cvsStream.str();
      cvsStream.str("");
  
      auto outputRslt1 = std::async (processing,1000,1);
      auto outputRslt2 = std::async (processing,1000,2);
      auto outputRslt3 = std::async (processing,1000,3);
  
      stringstream rsltThread1 = outputRslt1.get();
      stringstream rsltThread2 = outputRslt2.get();
      stringstream rsltThread3 = outputRslt3.get();
  
      // close csv file
      myfile << rsltThread1.str();
      myfile << rsltThread2.str();
      myfile << rsltThread3.str();
  
      myfile.close();
  
  
      return 0;
  }


errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
main.cpp: In function ‘std::stringstream processing(int, int)’:
main.cpp:22:8: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
 return cvsStream;
        ^
In file included from main.cpp:4:0:
/usr/include/c++/4.8.2/sstream:502:11: note: ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_stringstream : public basic_iostream<_CharT, _Traits>
           ^
/usr/include/c++/4.8.2/sstream:502:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.8.2/iostream:40:0,
                 from main.cpp:1:
/usr/include/c++/4.8.2/istream:795:11: note: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_iostream
           ^
/usr/include/c++/4.8.2/istream:795:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.8.2/istream:58:11: note: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_istream : virtual public basic_ios<_CharT, _Traits>

Last edited on
Streams are not copyable. C++11 made them moveable and this is what makes it possible to return a local stringstream object from a function. Unfortunately GCC 4.8 had not yet added the move constructor that was necessary for this to work.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

Consider solving your problem some other way, perhaps by making the function return the result as a string, or upgrade to a later version of GCC.
Topic archived. No new replies allowed.