gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:37:45 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com