iKy*_*aki 2 c++ metaprogramming
我刚刚开始阅读Accelerated C++,当我遇到这个时,我正在努力完成练习:
0-4. Write a program that, when run, writes the Hello, world! program as its output.
所以我提出了这个代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << helloWorld << endl;
cin.get();
return 0;
}
void helloWorld(void)
{
cout << "Hello, world!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我一直在收到错误'helloWorld' : undeclared identifier
.我想我应该做的是为helloWorld创建一个函数,然后为输出调用该函数,但显然这不是我需要的.我也试过投入helloWorld()
主力,但这也没有帮助.任何帮助是极大的赞赏.
Cod*_*ice 11
我阅读课本练习的方式是,它希望你编写一个程序,将另一个 C++程序打印到屏幕上.现在,您需要使用s cout
包围的大量语句和文字字符串来完成此操作""
.例如,您可以从
cout << "#include <iostream>" << std::endl;
Run Code Online (Sandbox Code Playgroud)
你实际上并没有在helloWorld
任何地方调用你的功能.怎么样:
int main()
{
helloWorld(); // Call function
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意:如果要在定义之前使用它,还需要在顶部声明函数原型.
void helloWorld(void);
Run Code Online (Sandbox Code Playgroud)
这是一个工作样本.