Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a declaration conflict with itself?

Tags:

c++

g++

solver

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?

like image 437
eeeeaaii Avatar asked Aug 24 '10 02:08

eeeeaaii


1 Answers

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
like image 67
Harper Shelby Avatar answered Sep 28 '22 08:09

Harper Shelby