简体   繁体   English

重新申报错误

[英]Redeclaration Error

I have understood the difference between declaration and definition And I was practicing some question when I hit the doubt, the below code asked me to list out the error in the snippet. 我理解了声明和定义之间的区别当我遇到疑问时,我正在练习一些问题,下面的代码要求我列出代码片段中的错误。

f(int a,int b)
{
     int a;
     a=20;
     return a; 
}

Why does this gives re-declaration error of a ? 为什么这给重新申报错误a Shouldn't it give multiple definition of a because in: 难道不应该给的多个定义a因为:

  • f(int a,int b) — here a is defined right? f(int a,int b) -这里a定义吗?
  • and in the function body, int a is defined again? 在函数体中, int a被定义了吗?

So why not multiple definition error? 那为什么不是多重定义错误呢?

A definition is always a declaration. 定义始终是一个声明。 The difference is that a definition also gives whatever you declare some value. 不同之处在于定义也给出了声明某些值的任何内容。

In your example, by the way, it is only a redeclaration error: 在你的榜样,顺便说一下,它仅仅一个重新声明错误:

f(int a, /* Defines a */
int b)
{
     int a; /* Declares a - error! */
     a=20; /* initializes a */
     return a; 
}

You probably meant to do this: 你可能打算这样做:

f(int a, /* Defines a */
int b)
{
     int a = 20; /* Declares and defines a - error! */
     return a; 
}

But in this case, most compilers will throw a "redeclaration" error too. 但在这种情况下,大多数编译器也会抛出“重新声明”错误。 For example, GCC throws the following error: 例如,GCC抛出以下错误:

Error: 'a' redeclared as a different kind of symbol 错误:'a'重新声明为另一种符号

That is because a is originally defined as a parameter, which is different from a variable definition inside the function's scope. 这是因为a最初定义为参数,它与函数范围内的变量定义不同。 As the compiler sees that you're re-declaring something that is of a different "breed" than your new declaration, it can't care less if your illegal declaration is a definition or not, because it regards "definition" differently in terms of function parameters and function local variables. 由于编译器发现你重新声明了与你的新声明不同的“品种”的东西,如果你的非法声明是一个定义,它可以不在乎,因为它在术语上对“定义”的看法不同函数参数和函数局部变量。

However, if you do this: 但是,如果你这样做:

int c = 20;
int c = 20;

GCC , for example, throws a redefinition error, because both c -s are the function's local variables. 例如, GCC会抛出重定义错误,因为两个c都是函数的局部变量。

When you declare a variable in your code,a block of memory will be allocated with that variable name depending on the data type used in your declaration. 在代码中声明变量时,将根据声明中使用的数据类型为该变量名分配一块内存。

But when you try to redeclare the same variable the processor tries to allocate the memory which is already allocated with the same name.so as the processor face ambiguity while trying to access the memory block with that variable name the compiler do not allow that instruction, hence multiple declarations will not be allowed and you will get a error in GCC compiler telling, 但是当你尝试重新声明同一个变量时,处理器会尝试分配已经分配了相同名称的内存。因为当尝试访问具有该变量名称的内存块时处理器面临歧义,编译器不允许该指令,因此不允许多个声明 ,你会在GCC编译器中得到错误,

line 3 col 10    [Error]redeclaration of 'int a'
line 1 col 7     [Error]'int a' previously declared here

in your code 在你的代码中

  f(int a,int b) //first declaration of 'a' { int a; //redeclaration of 'a', whose memory is already allocated a=20; return a; } 

on the memory layout two blocks cannot have same identity(variable name), hence the compiler throws a redeclaration error as multiple declarations are not possible, when variable 'a' is redeclared. 在内存布局上,两个块不能具有相同的标识(变量名),因此当重新声明变量'a'时,编译器会抛出重新声明错误,因为多个声明是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM