开发者

c compilation error

These are my errors:

error: static declaration of doct follows non-static declaration

error: previous declaration of doct was here.

And my code is:

int doct(int*); /* <- Second error points here */

private int doct(int *a)
  {
    static int a=0; /* First error points here */
    a++;
    *a=a;
    return开发者_高级运维 0;
  }

Any suggestions?


Your prototypes should match your actual functions. Yours do not:

int doct(int*);
private int doct (int *a)

Either change the prototype to:

private int doct(int*);

or change the function to:

int doct (int *a)

You should also bear in mind that private is not part of the C language, but people often use it to replace static. This can be made possible by the line:

#define private static

with the only proviso being that that macro must be active wherever you use the private name. If it's not working on your prototype, that's probably because it's not defined at that point. My advice would be to ditch private altogether and use static (if indeed that's how private is defined). People should learn the language, not adopt unnecessary crutches (in my opinion).

Other favourites which I also despise are:

#define global extern
#define begin {
#define end }

The private and global are used to mean local to this file and global to all files respectively. The begin and end are particularly nasty abominations from people who should go back to Pascal where they belong :-)

In addition to that problem, your line:

static int a = 0;

will actually hide the parameter that you're passing into the function (since it has the same name) and:

*a = a;

will cause an error (since it has a different type). It's rarely a good idea to do that. Rename one of them.


This error happens when a function was declared as non-static, then defined static, such as:

void foo(void);

static void foo(void) {}

Make static match on both, either by removing it from both or adding it to both. Make sure you understand what static does.

If your function is marked static, it is only visible in that translation unit. In your case, your declaration has no static meaning "this function will be available, non-statically.", but then you define it statically.


There are other errors. The a in your function will hide the a in the parameter list. You need to give them different names. *a = a won't work because, in that scope, a is an integer, not a pointer. Use a descriptive name like counter for the integer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜