write to file using a function from a custom class

so i need to write out my results to a text file named results. i'm not sure how to translate my code which just uses cout. i thought i could just change cout to out from ofstream but out is not defined is the error. can someone please assist me? my main, header and class cpp files are below

main code below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "myclass.h"
#include <fstream>

using namespace std;

int main()
{
  ifstream in;
    ofstream out;
    out.open("results.txt");
    in.open("input.txt");
    int h;
    cin>>h; //100 input
    
    
    myclass a;
    a.twoPlayers(h);
    return 0;
}


header file
myclass.h

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
#ifndef MYCLASS_H
#define MYCLASS_H


class myclass
{
    public:
        myclass();
        int die();
        int snakeTail(int a);
        int snakeHead(int a);
        int ladderBottom(int a);
        int ladderTop(int a);
        int checkLad(int &positionN1,int lb1,int lb2,int lb3,int lb4,int lb5,int lb6,int lb7,int lb8,
          int lt1,int lt2,int lt3,int lt4,int lt5,int lt6,int lt7,int lt8,int p,int round,int die);
        int checkSn(int &positionN1,int sh1,int sh2,int sh3,int sh4,int sh5,int sh6,int sh7,int sh8,
            int st1,int st2,int st3,int st4,int st5,int st6,int st7,int st8,int p,int round,int die);
        int noverh(int &positionN1, int dice);
        void twoPlayers(int h);
    protected:

    private:
        int num;
};

#endif // MYCLASS_H 


mistake
Last edited on
mistake
Last edited on
Pass out as a param to the class constructor, and save it's value in the class as a type ostream& (say ofs) in the constructor initialisation.

Then whenever you currently use cout in the class, use ofs instead. Everything now written to cout will then go to the file specified by ofs. You can default the constructor to cout if you want.

Something like (not tried):

1
2
3
4
5
6
7
private:
    ostream& ofs;

myclass::myclass(ostream& ofs_ = cout) : ofs(ofs_)
{
    //ctor
}


i use void function to write out to the file and in that function theres another function that writes out to the same file if a condition is met, if i do what you say should it still work?
so do i just do this if so its giving me errors. can you please clarify
add this to my header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef MYCLASS_H
#define MYCLASS_H


class myclass
{
    public:
      
       myclass(ostream& ofs_ = cout) : ofs(ofs_)
       //rest of my functions
       
    protected:

    private:
         ostream& ofs;
};


in myclass.cpp file

1
2
3
4
myclass::myclass(ostream& ofs_ = cout) : ofs(ofs_)
{
    //ctor
}


Have you checked that the file is open and available to be written to? I only ask bc you should definitely be able to push data to it just like any other stream. If you're attempting to use it inside of a class I find it best to use an overloaded operator.

What is the error?
error: expected ')' before '&' token
error :ostream does not name a type
when i try to compile what i have above
(1) You're trying to implement the constructor in both the header and the .cpp file. Normally, just declare it in the header.
(2) ostream is part of the std namespace, so you should use std::ostream in the header
(3) cout is a global object that comes from <iostream> so you need to #include <iostream> before referencing std::cout.
(4) The default parameter is part of the interface and should be in the header, not the implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>

class myclass
{
    public:     
       myclass(std::ostream& ofs_ = std::cout);
       std::ostream& ofs;
};

myclass::myclass(std::ostream& ofs_) : ofs(ofs_)
{
    
}

int main()
{
    myclass myc;
}
Last edited on
this all seems to be working...

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

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

class myclass {
public:
	ofstream* out;
	myclass(ofstream& o, string& s){
		out=&o;
		out->open(s);
	}

	void pumpANDdump(string s) {
		*out << s;
	}

	void close() { out->close(); }

};

int main() {
	ofstream of_str;
	string ss = "outfile.txt";

	myclass my(of_str,ss);
	my.pumpANDdump((string)"pumpAndDump");
	my.close();
}

Last edited on
Topic archived. No new replies allowed.