C++ 给ostream赋值

C++ 给ostream赋值,c++,ostream,C++,Ostream,下面是解析for语句的代码,但我不确定在调用方法write(…)时如何将任何值放入ostream。我能做什么?(例如write(“for(……)) #包括 #包括 使用名称空间std; //我在这里删掉了声明部分 typedef常量字符串类型; 私人: 类型*初始值设定项; 类型*条件; 类型*增量; 类型*主体; 公众: 无效写入(ostream和stream)常量{ 流动 我猜您试图学习使用ostream作为函数的输入。但似乎您将如何使用classs和方法混为一谈。 也许这没有用,但我可以给

下面是解析for语句的代码,但我不确定在调用方法
write(…)
时如何将任何值放入
ostream
。我能做什么?(例如
write(“for(……))

#包括
#包括
使用名称空间std;
//我在这里删掉了声明部分
typedef常量字符串类型;
私人:
类型*初始值设定项;
类型*条件;
类型*增量;
类型*主体;
公众:
无效写入(ostream和stream)常量{
流动

我猜您试图学习使用
ostream
作为函数的输入。但似乎您将如何使用
class
s和
方法混为一谈。
也许这没有用,但我可以给你一个小片段,给你一些意见

#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;
}
#包括
#包括
使用名称空间std;
typedef常量字符串类型;
类型*init;
类型*cond;
类型*incr;
类型*主体;
无效写入(ostream和stream){
流动

我猜您试图学习使用
ostream
作为函数的输入。但似乎您将如何使用
class
s和
方法混为一谈。
也许这没有用,但我可以给你一个小片段,给你一些意见

#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;
}
#包括
#包括
使用名称空间std;
typedef常量字符串类型;
类型*init;
类型*cond;
类型*incr;
类型*主体;
无效写入(ostream和stream){
流动

我猜您试图学习使用
ostream
作为函数的输入。但似乎您将如何使用
class
s和
方法混为一谈。
也许这没有用,但我可以给你一个小片段,给你一些意见

#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;
}
#包括
#包括
使用名称空间std;
typedef常量字符串类型;
类型*init;
类型*cond;
类型*incr;
类型*主体;
无效写入(ostream和stream){
流动

我猜您试图学习使用
ostream
作为函数的输入。但似乎您将如何使用
class
s和
方法混为一谈。
也许这没有用,但我可以给你一个小片段,给你一些意见

#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;
}
#包括
#包括
使用名称空间std;
typedef常量字符串类型;
类型*init;
类型*cond;
类型*incr;
类型*主体;
无效写入(ostream和stream){
流动


通常,编写一个实际的解析器来完成这个任务。C++太复杂了,无法解析(无论这个是什么)。@user2976089不,因为您使用的是
ostream
,而
ifstream
istream
@user2976089:不,这是用于读取文件的。此函数旨在将数据写入某种输出。看起来更像是粘贴在一起的代码片段……您的问题是什么?实际代码是否编译?你得到了什么输出?你期待什么?@ USER 29 76089:这只是错误消息的一部分。通常你编写一个实际的解析器来完成这个任务。C++太复杂了,无法像这样解析(无论这个是什么)。@user2976089不,因为您使用的是
ostream
,而
ifstream
istream
@user2976089:不,这是用于读取文件的。此函数旨在将数据写入某种输出。看起来更像是粘贴在一起的代码片段……您的问题是什么?实际代码是否编译?你得到了什么输出?你期待什么?@ USER 29 76089:这只是错误消息的一部分。通常你编写一个实际的解析器来完成这个任务。C++太复杂了,无法像这样解析(无论这个是什么)。@user2976089不,因为您使用的是
ostream
,而
ifstream
istream
@user2976089:不,这是用于读取文件的。此函数旨在将数据写入某种输出。看起来更像是粘贴在一起的代码片段……您的问题是什么?实际代码是否编译?你得到了什么输出?你期待什么?@ USER 29 76089:这只是错误消息的一部分。通常你编写一个实际的解析器来完成这个任务。C++太复杂了,无法像这样解析(无论这个是什么)。@user2976089不,因为您使用的是
ostream
,而
ifstream
istream
@user2976089:不,这是用于读取文件的。此函数旨在将数据写入某种输出。看起来更像是粘贴在一起的代码片段……您的问题是什么?实际代码是否编译?您得到了什么输出?您期望得到什么?@user2976089:这只是错误消息的一部分。当没有输出任何内容时,
write(cout)
函数如何工作?没有输出任何内容?它实际上输出了一个for循环块。尝试在return语句之前添加
cin>>argc;
。函数如何写入(cout)
工作,当没有输出任何内容?没有输出任何内容?它实际上输出一个for循环块。尝试在return语句之前添加
cin>>argc;
。函数
如何写入(cout)
工作,当没有输出任何内容?没有输出任何内容?它实际上输出一个for循环块。尝试在return语句之前添加
cin>>argc;
。函数
如何写入(cout)
work,当没有输出任何内容?没有输出任何内容?它实际上输出一个for循环块。尝试在return语句之前添加
cin>>argc;