質問

これは、TAUCS(私のコードではなく)を使用するコードをコンパイルしようとしているときに得られるエラーです。

.../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’

ワット?それはそれ自体と矛盾していますか?

私が自分自身をつまんだ後、私はテストヘッダーを作成し、競合する定義を導入しました。

ファイルtestit.h:

#include "somethingelse.h"

typedef struct
{
  int n;
} foobar;

ファイルでsomeingelse.h:

typedef struct
{
  int n;
} foobar;

案の定、私は得ます:

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

または、私が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’

行番号は常に異なります - 宣言はそれ自体と矛盾することはできません。理解できません。誰かがこれを見たことがありますか?

役に立ちましたか?

解決

ヘッダーファイル(.../taucs/src/taucs.h)、宣言を含む)は、(直接的または間接的に)2個の別々の2回によって含まれています #include 指令?

他のヒント

単一のヘッダーは複数のソースファイルに含まれていますか?もしそうなら、あなたはそれをそうするような「ガードを含む」でそれを包む必要があります:

#ifndef TAUCS_H
#define TAUCS_H

//Header stuff here

#endif //TAUCS_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’

この例では、Foobarの2つの宣言を示します。コンパイラはどちらを選択するかわからないので、矛盾する宣言で救済します。同じことを二度宣言することはできません。

定義を繰り返さないでください。 C ++では、定義が1回しか表示されません。宣言を繰り返すことです。

a typedef 常に定義です。だから私がお勧めする最初のことは struct 適切な名前(そしてこれはC ++であるため、typedefは利点を追加しないので、typedefをドロップするだけです):

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

次に、それは正確に1つのファイルにあるはずです。 FoobarにPointersのみを使用するファイルがある場合は、 宣言を繰り返します (定義ではありません):

// file2.h

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

void doit(const foobar *f); 

私は私のコードを並べて同じ問題を抱えていました、そしてそれはそうでした いいえ タイプの二重宣言。 PC-Lintは、混合CおよびC ++コードで使用されているのと同じTypedefについて不満を述べました。同じ宣言を避けることで、それを修正できました C ++ファイル。それが誰かを助けることを願っています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top