views:

135

answers:

2

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?

+2  A: 

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.

GMan
but in my function there is no static keyword.
ambika
@ambika: You have `private`. `private` is *not* part of the C language, so I can't give you anything else. To generate that error though, `private` must be a macro for `static`. That is: `#define private static`. So put "`private`" in the declaration too, or remove "`private`" from the definition. I would recommend you use `static` instead of `private`, because it's confusing (just look at the responses to it on this question. :] )
GMan
thanks GManyou are super.i solve that.
ambika
+3  A: 

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.

paxdiablo
i make int doct(int*);toprivate int doct(int*);it doesn't work.
ambika
What does "doesn't work" mean? I your declaration was in a header, it probably won't work. Is it in a header?
GMan
I'd love to know what does "private" keyword mean in this context, at first :)
Roman D