How can a declaration conflict with itself?
Asked Answered
J

5

9

This is the error I'm getting when trying to compile some code that uses taucs (not my code):

.../taucs/src/taucs.h:554: error: conflicting declaration ‘typedef struct taucs_ccs_matrix taucs_ccs_matrix’
.../taucs/src/taucs.h:554: error: ‘taucs_ccs_matrix’ has a previous declaration as ‘typedef struct taucs_ccs_matrix taucs_ccs_matrix’

wat? It is conflicting with itself?

After I pinched myself, I created a test header and put in a conflicting definition, just to make sure I was right about this:

In file testit.h:

#include "somethingelse.h"

typedef struct
{
  int n;
} foobar;

In file somethingelse.h:

typedef struct
{
  int n;
} foobar;

Sure enough, I get:

testit.h:6: error: conflicting declaration ‘typedef struct foobar foobar’
somethingelse.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’

Or if I have this in testit.h:

typedef struct
{
  int n;
} foobar;

typedef struct
{
  int n;
} foobar;

testit.h:9: error: conflicting declaration ‘typedef struct foobar foobar’
testit.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’

The line number is always different -- a declaration can't conflict with itself. I don't get it. Anyone ever seen this?

Jinajingle answered 24/8, 2010 at 2:58 Comment(0)
I
7

Could it be that your header file (.../taucs/src/taucs.h), which contains the declaration, is (directly or indirectly) included twice by two separate #include directives?

Intercalate answered 24/8, 2010 at 3:2 Comment(1)
I decided to give the checkmark to this answer since it came first.Jinajingle
F
16

Is the single header included in multiple source files? If so, you need to wrap it in "include guards" like so:

#ifndef TAUCS_H
#define TAUCS_H

//Header stuff here

#endif //TAUCS_H
Flamsteed answered 24/8, 2010 at 3:3 Comment(2)
You should avoid the leading double underscores, though. Those names are reserved.Bizarre
So are the trailing double underscores. A correct form would be TAUCS_H. C and C++ programmers won't use macros with _H suffixes for other purposes, so this is safe enough without further decoration.Vortical
I
7

Could it be that your header file (.../taucs/src/taucs.h), which contains the declaration, is (directly or indirectly) included twice by two separate #include directives?

Intercalate answered 24/8, 2010 at 3:2 Comment(1)
I decided to give the checkmark to this answer since it came first.Jinajingle
I
1
typedef struct
{
   int n;
} foobar;

typedef struct
{
   int n;
} foobar;

testit.h:9: error: conflicting declaration ‘typedef struct foobar foobar’
testit.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’

In this example you give 2 declarations of foobar. The compiler does not know which one to choose - so it bails out with conflicting declaration. You can't declare the same thing twice.

Intelligent answered 24/8, 2010 at 5:17 Comment(0)
W
1

Don't repeat the definition. C++ allows a definition to only appear one time. What you can do is repeat a declaration.

A typedef is always a definition. So the first thing I would recommend is giving the struct proper a name (and since this is C++, a typedef does not add any benefit so just drop the typedef):

// file1.h
struct foobar
{
    int n;
};

Next, that should be in exactly one file. If you have files that only use pointers to foobar, you can repeat the declaration (just not the definition):

// file2.h

// This is just a declaration so this can appear as many times as
// you want
struct foobar;

void doit(const foobar *f); 
Weatherboard answered 24/8, 2010 at 5:30 Comment(0)
F
0

I had the same issue linting my code and it was not double declaration of a type. PC-Lint complained about the same typedef being used in mixed C and C++ code. I could fix that by avoiding the same declaration in C and C++ files. Hope that helps someone.

Fallingout answered 17/5, 2016 at 14:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.