开发者

type definition in parameter list

int func(struct x{int a;}y);
int main()
{

}

The above code gives following error in c++

error: types may not be defined in parameter types

while its run fine in 开发者_开发问答c with warning,means in c type may be define in parameter list

thus the following code should give redefination error in c

int func(int a,int a);
int main()
{

}

Doubt:why the above code giving error in c++

error:Multiple parameter named a


It's an error to have a parameter have the same name twice, because parameter names are in scope in their parameter list. I think it's obvious that having two parameters have the same name isn't useful?

// Explain why you want to have them say "int a, int a"
int func(int a, int b);

In C it is allowed to define the content of struct types in parameter type lists. The tag identifier either has prototype scope or block scope (when the function is a definition) and has no linkage, which means it is different from a struct declared outside the prototype or blocks, whose tag identifier has file scope. You cannot call such a function except recursively from inside its body.

int func(struct x{int a;} y) { 
  func(y); // fine (the call, not the endless recursion)
}

Note that this applies only for calls that happen in the same translation unit that func is defined in. If in another translation unit you declare the function and struct as follows, that is entirely fine, and the function will be callable (you need to make sure the above declaration is not visible, otherwise you get a clash, because x at both points denotes a different type).

struct x {int a;};
int func(struct x y);

In both translation units, even though x is a different type (because they are different translation units), the types are compatible, which is the only thing that matters for parameter types.

You cannot only define the struct in a parameter list, but also in the return type section of a function

struct x {int a;} func(void) {
  return (struct x){ 0 };
}

This too applies only to C. No such thing is allowed in C++.


I am not sure what you are trying to do. But if, the function func needs to take an argument of type struct x then -

struct x
{
  // ....
};

int func( struct x obj ) ;
int func( int a, int b ) ; // Notice that the second parameter is changed
                           // from a to b. Both the parameters cannot have
                           // the same name

Ok, you are trying to define a struct as a part of function argument. Had if there been such a possibility, the definition would be local to that function. How are you going to pass the parameter while on call to that function, since the struct x is not visible any where else other than in func.


I'm not sure either. But if you're asking why such an error, while before it was about type definition

  • in the first case c++ found syntax which is typical for a type definition, which is not allowed in c++ (watch Mahesh how does it should look)
  • the second do not contain any type definition, so it do not complain about it, but about an other kind of problem

... still I doubt in understanding the question

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜