定义一个 t 函数:输出 hello world
#include<iostream>
using namespace std;
void t();
int main() {
t(); //要加括号(形参表)
return 0;
}
void t() {
cout << "hello world";
}
将函数声明放入一个头文件 (.h)
将函数定义放入一个源文件 (.cpp)
注意:
函数声明与函数定义中用到了<iostream>头文件和命名空间
所以要分别在头文件和源文件中加入
#include<iostream>
using namespace std;
在源文件中可以通过添加 t.h(函数声明所在的头文件) ,来调用 t 函数
添加头文件有两种方式:
1.只可以添加系统中自带的头文件
#include<头文件名>
2.可以添加系统中自带的头文件和自己创建的头文件
#include"头文件名"
添加后
t.h 文件中
#include<iostream>
using namespace std;
//需要头文件和命名空间
void t();
t.cpp文件中
#include<iostream>
using namespace std;
//需要头文件和命名空间
void t() {
cout << "hello world";
}
源.cpp 文件中
#include<iostream>
using namespace std;
#include"t.h"
int main() {
t();//要加括号(形参表)
return 0;
}