As programs get larger
来源:互联网 发布:彩票网络开售最新消息 编辑:程序博客网 时间:2024/05/18 02:07
As programs get larger, it is not uncommon to split them into multiple files for organizational purposes. One advantage of working with an IDE is they make working with multiple files much easier. You already know how to create and compile single-file projects. Adding new files to existing projects is very easy.
In Visual Studio 2005 Express, right click on “Source Files” in the Solution Explorer window on the left, and choose Add -> New Item. Give the new file a name, and it will be added to your project.
In Code::Blocks, go to the file menu and choose “new file”. Give the new file a name, and Code::Blocks will ask you if you want to add it to the active project. Click “Yes”. Note that you will also have to click the “Release” and “Debug” checkboxes, to make sure it gets added to both versions.
Compile your project just the same as before. Couldn’t be much easier!
Now, consider the following multiple-file program:
add.cpp:
int
add(
int
x,
int
y)
{
return
x + y;
}
main.cpp:
#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:
When the compiler is compiling a code file, it does not know about the existence of functions that live in any other files. This is done so that files may have functions or variables that have the same names as those in other files without causing a conflict.
However, in this case, we want main.cpp to know about (and use) the add() function that lives in add.cpp. To give main.cpp access to the add function, we can use a forward declaration:
main.cpp with forward declaration:
#include <iostream>
int
add(
int
x,
int
y);
// forward declaration using function prototype
int
main()
{
using
namespace
std;
cout <<
"The sum of 3 and 4 is: "
<< add(3, 4) << endl;
return
0;
}
Now, when the compiler is compiling main.cpp, it will know what add is. Using this method, we can give files access to functions that live in another file. However, as programs grow larger and larger, it becomes tedious to have to forward declare every function you use that lives in a different file. To solve that problem, the concept of header files was introduced. We discuss header files in the lesson on header files.
Try compiling add.cpp and the main.cpp with the forward declaration for yourself. We will begin working with multiple files a lot once we get into object-oriented programming, so now’s as good a time as any to make sure you understand how to add and compile multiple file projects.
- As programs get larger,
- As programs get larger
- Programs as Data: Function Pointers
- Programs as Data: Function Pointers(函数指针)
- These critical programs are missing or too old: as ld
- Get StackTrace as String
- Get Users Involved As Early As Possible
- ORA-15099 --Bug 10072750 AIX: ASM sees 1 TB disks as larger than their actual size
- as,build时出现the Gradle daemon needs a larger heap
- QTP: Get Data as Collection
- 关于编译glibc-2.9的These critical programs are missing or too old: as ld 错误
- Ubuntu安装crosstool These critical programs are missing or too old: as ld错误
- 【转】crosstool These critical programs are missing or too old: as ld 问题
- Ubuntu 10.04 安装crosstool These critical programs are missing or too old: as ld错误
- c5 URLs and URIs - Communicating with Server-Side Programs Through GET
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- 9.1 Programs
- Useful programs
- 两个类相互包含引用的问题
- PL2303芯片实现串口转usb,但是要在电脑上装上芯片的驱动
- Selector中各种状态详解
- hdu1231 最大连续子序列
- 英语在未来考试中的地位
- As programs get larger
- The preprocessor
- B. Valera and Fruits
- ubuntu中root权限的获得及arm-linux-gcc交叉编译环境的搭建
- Filthy Rich
- F广搜
- ecshop源码分析:smarty模板类
- Java经典23种设计模式之行为型模式(二)
- JSON学习笔记