随着程序变得更大,这种情况并不少见组织的目的将它们分割成多个文件。 带有IDE工作的一大优势是他们让多个文件更容易工作。 你已经知道如何创建和编译单个文件的项目。 现有项目添加新文件是很容易的。
在Visual Studio 2005 Express中,右键单击“源文件”在解决方案资源管理器窗口的左边,选择“添加” - >“新建项目”。 给新的文件名称,它将被添加到您的项目。
在Code :: Blocks的,“文件”菜单中,选择“新文件”。 给新的文件名称,Code :: Blocks的会问你,如果你想将它添加到活动项目。 点击“是”。 请注意,您也将有点击“发布”和“调试”复选框,以确保它被添加到这两个版本。
编译您的项目只是像以前一样。 无法容易得多!
现在,考虑下面的多档节目:
add.cpp:
1
2
3
4
int add(int x, int y)
{
return x + y;
}
main.cpp:
1
2
3
4
5
6
7
8
#include <iostream>
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
Try compiling this program for yourself. You will note that it doesn’t compile, and it gives the same compiler error as the program in the previous lesson where the functions were declared in the wrong order: