Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
56 views
in Technique[技术] by (71.8m points)

Strange error in C++- operation << in ofstream can't work with strings

In the following code I got somehow the following error (C++11,g+) :

template < typename T >static void write_csv(vector<pair< string, vector<T> > >  dataset){

    ofstream myFile("Results");

    for(int j = 0; j < dataset.size(); ++j){
        myFile << dataset.at(j).first;
        if(j != dataset.size() - 1) myFile << ","; // No comma at end of line
    }
    myFile << "
";
}

The error is error: no match for 'operator<<' (operand types are 'std::ofstream {aka ?std::basic_ofstream<char>}' and 'const char[2]. ?

Also, I am attaching my includes for the program.

#include <math.h>
#include <vector>
#include <map>
#include <limits>
#include <climits>
#include <algorithm>   // for std::sort
#include <string.h>    // for memset
#include <stdio.h>     // for printf
#include <assert.h>
#include <time.h>
#include <iostream>
#include <ostream>

#include <stdlib.h>
#include <random>
#include <cmath>
#include <gsl/gsl_math.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>

using namespace std;

What can be the problem?

question from:https://stackoverflow.com/questions/65872064/strange-error-in-c-operation-in-ofstream-cant-work-with-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...