i want to output a matrix into a txt file using a display function ( i tried to do it without the displayDor function and still didn't work) the error is in the last line inside the function enregistrer line 48
and its saying : [Error] no match for 'operator<<' (operand types are 'std::ofstream {aka std::basic_ofstream}' and 'void')
the code is the following :
01 -#include <iostream>
02 -#include<ctime>
03 -#include<cstdlib>
04 -#include<fstream>
05 -using namespace std;
06 -struct Case{
07 - bool N,W,S,E;
08 - };
09 -
10 -struct laby{
11 - int p;
12 - int q;
13 - Case **tab;
14 - };
15 -
16 -laby *cree_laby(int p,int q){
17 - laby *labyr=new laby;
18 - labyr->p=p;
19 - labyr->q=q;
20 - labyr->tab=new Case*[p];
21 - for(int i=0;i<p;i ){
22 - labyr->tab[i]=new Case[q];
23 - for(int j=0;j<q;j ){
24 - labyr->tab[i][j].N=false;
25 - labyr->tab[i][j].W=false;
26 - labyr->tab[i][j].S=false;
27 - labyr->tab[i][j].E=false;
28 - }
29 - }
30 - return labyr;
31 - }
32 -
33 -void displayLaby(const laby &labyr){
34 - for(int i=0;i<labyr.p;i ){
35 - for(int j=0;j<labyr.q;j ){
36 - cout<<'['<<labyr.tab[i][j].N<<','<<labyr.tab[i][j].E<<','<<labyr.tab[i][j].S<<','<<labyr.tab[i][j].W<<']';
37 - }
38 - cout<<"\n";
39 - }
40 - }
41 -
42 -void enregistrer(laby & labyr,char *filename){
43 - ofstream f;
44 - f.open(filename);
45 - f<<labyr.p;
46 - f<<labyr.q;
47 - f<<endl;
48 - f<<displayLaby(labyr);
49 -}
50 -
51 -int main(){
52 - laby *labyr;
53 - labyr=cree_laby(5,5);
54 - enregistrer(*labyr,"laby.txt");
55 - return 0;
56 - }
i tried to use a function display and i used loops also to output the maze into a txt file but they are both not working,
i would appreciate the help, Thank you,
CodePudding user response:
Your displayLaby
writes to std::cout
, if you want it to write somewhere else (like a file) then you have to pass the stream as a parameter to the function. Like this
void displayLaby(const laby &labyr, ostream& os){
for(int i=0;i<labyr.p;i ){
for(int j=0;j<labyr.q;j ){
os<<'['<<labyr.tab[i][j].N<<','<<labyr.tab[i][j].E<<','<<labyr.tab[i][j].S<<','<<labyr.tab[i][j].W<<']';
}
os<<"\n";
}
}
void enregistrer(laby & labyr,char *filename){
ofstream f;
f.open(filename);
f<<labyr.p;
f<<labyr.q;
f<<endl;
displayLaby(labyr, f);
}
Here os
is the new parameter, and writes go to that stream instead of cout
. The type of the new parameter is ostream&
which means it will work for any kind of output stream. Of course if you want to write to cout
anyway then you just call the new function like this displayLaby(labyr, cout);
.