user2976089
user2976089

Reputation: 355

Putting a value to ostream

I have this code below which parses a for statement, but I am not sure how to put any value into the ostream when calling the method write(...). What can I do? (e.g write("for (........."))

#include <ostream>
#include <iostream>

using namespace std;

//I cut out the declaration bit here

typedef const string type;

private:
type *initializer;
type *condition;
type *increment;
type *body;

public:
void write(ostream& stream) const {
      stream
        << "for ("
        << *initializer << "; "
        << *condition << "; "
        << *increment << ")\n{\n"
        << *body
        << "}";
}

Upvotes: 0

Views: 604

Answers (1)

Fredrick Gauss
Fredrick Gauss

Reputation: 5166

I guess you try to learn using ostream as an input in a function. But it seems that you mixing things that how to use classs and methods. Maybe this is no avail but i can give you a little snippet to give you some opinion.

#include <iostream>
#include <string>

using namespace std;

typedef const string type;

type *init;
type *cond;
type *incr;
type *body;


void write(ostream& stream) {
      stream
        << "for ("
        << *init << "; "
        << *cond << "; "
        << *incr << ")\n{\n"
        << *body
        << "\n}";
}


int main(int argc, char* argv[])
{
    const string ini = "int i = 0";
    const string con = "i < 10";
    const string inc = "i++";
    const string bod = "cout << i << endl;";

    init = &ini;
    cond = &con;
    incr = &inc;
    body = &bod;

    write(cout);

    return 0;
}

Try this code, examine and read more for more details.

Upvotes: 3

Related Questions