编译器从哪里开始读取 [英] From where does the compiler start reading

查看:116
本文介绍了编译器从哪里开始读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个小程序:

  #include< iostream> 
using namespace std;

int main(){
f();
system(pause);
}

void f(){
static int x = 20;
class tester {
public:
tester(){
cout< X ;
}
} x1;
}

我得到的错误是: error C3861:'f ':identifier not found



如果我放置函数 f
我被告知程序执行开始于 main 。



。根据这个代码应该也运行在第一种情况下。



编译器如何开始阅读程序?

解决方案

编译的开始和程序的执行开始是两个不同的事情。



执行开始于 main



编译从文件开头开始;编译器不会跳过文件来找到所需的片段,但它以线性方式读取输入(我怀疑这是相关的,其他事情,C ++语法真的很复杂)。



当编译器在解析文件的某个时候,它只知道到目前为止

/ p>

因此,已经发明了函数原型(和一般的非定义的声明):定义了所有函数的原型放在文件的开头,通常在 #include 指令之后或在单独的包含文件中。原型告诉编译器这样的函数将在后面定义,什么是函数签名(即名称,参数,返回值)。



一个正常的函数,但没有正文,它被一个分号 2 替换。例如,在您的代码中,您将写入

  void f  main 之前的



<





  1. IIRC有一些放宽规则允许编译器等待


  2. 在一个原型中也常见的不是写参数的名称,而是离开只是它们的类型(这也可以在函数定义,但它没有什么意义,除非你有一个形式的参数,你不使用)。仍然,我喜欢将参数名称保留为文档的形式。



This is a small program :

#include <iostream>
using namespace std;

int main() {
    f();
    system("pause");
}

void f() {
    static int x = 20 ;
    class tester {
    public :
        tester() {
            cout << x ;
        }
    } x1;
}

The error that i get here is :error C3861: 'f': identifier not found

If i place the function f above main I will get the desired output.

Why it is so ? I was told that program execution begins at main. According to this the code should run in the first case also.

How does the compiler start reading the program?

解决方案

The beginning of the compilation and the beginning of the execution of the program are two different things.

The execution starts from the main.

The compilation begins from the beginning of the file; the compiler don't "jump around" the file to find the needed pieces, but it reads the input in a linear fashion (I suspect that this related, among the other things, to the fact that the C++ grammar is really complicated).

When the compiler is at some point in parsing the file, it only knows what has been declared/defined up to that point1.

Because of this, function prototypes (and non-defining declarations in general) have been invented: the prototypes of all the functions defined in the file are put at the beginning of the file, typically after the #include directives or in a separated include file. The prototypes tell to the compiler that such functions will be defined later, and what is the function signature (i.e. name, parameters, return value).

The prototype is made as a normal function, but without the body, which is replaced by a semicolon2. For example, in your code you would write

void f();

before the main.


  1. IIRC there are some relaxations to this rule that allow the compiler to "wait" for some declarations to make some template magic work, but this is not relevant here.

  2. In a prototype is also common not to write the names of the parameters, leaving just their type (this can be done also in function definitions, but it doesn't make much sense there unless you have a formal parameter you don't use). Still, I prefer to leave the parameter names there as a form of documentation.

这篇关于编译器从哪里开始读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆