c++ - 使用 g++ 删除了 linux 中的 std basic_stringstream 函数

标签 c++ multithreading

我在 Qt IDE 上的 Windows 中编写了以下内容,当我运行它时,它运行良好,但是当我尝试在 CentOS 上运行它时,我想使用线程运行代码,其中我只是尝试在 centos 环境中加载 CSV 文件并将结果写入其中

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

我收到错误

这个问题有什么解决办法吗?

代码

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

错误

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>

最佳答案

事实上,Streams 是不可复制的,不幸的是 GCC 4.8 尚未添加其工作所需的移动构造函数。 C++11 使它们可移动,这使得从函数返回本地字符串流对象成为可能。

您可以使用函数将结果作为字符串返回并视为解决方案或 upgrade to a later version of GCC .

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

string 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.str();
}

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 outputRsl1 = std::async (processing,1000,1);
    auto outputRsl2 = std::async (processing,1000,2);
    auto outputRsl3 = std::async (processing,1000,3);

    string rslThread1 = outputRsl1.get();
    string rslThread2 = outputRsl2.get();
    string rslThread3 = outputRsl3.get();

    // close csv file
    myfile << rslThread1;
    myfile << rslThread2;
    myfile << rslThread3;

    myfile.close();

    return 0;
}

关于c++ - 使用 g++ 删除了 linux 中的 std basic_stringstream 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926506/

相关文章:

java - Java 中 while 和 for 的组合

c++ - Qt 线程出错

c++ - 在 C++ 中使用指向对象的指针和 operator new 来定义对象

c++ - 以下4种调用其他成员方法的方式有什么区别?

c++ - 将类型转换包装在 static_cast 中会造成伤害吗?

c# - 是 new Thread(() => {//logic}).Start();可接受在 Web 应用程序 page_load 中执行逻辑 "asynchronously"

使用 JNI 的 Java 守护线程

c++ - 在 CMake 的子目录中保持文件层次结构

c++ - 在 unique_ptr vector 中搜索时出现非标准扩展警告

c# - 使用多个 http 请求-响应