声明如何与自身发生冲突?

eee*_*aii 8 c++ g++ solver

这是我在尝试编译一些使用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’
Run Code Online (Sandbox Code Playgroud)

笏?它与自己相冲突?

在我捏自己之后,我创建了一个测试标题,并提出了一个相互冲突的定义,只是为了确保我对此是正确的:

在文件testit.h中:

#include "somethingelse.h"

typedef struct
{
  int n;
} foobar;
Run Code Online (Sandbox Code Playgroud)

在somethingelse.h文件中:

typedef struct
{
  int n;
} foobar;
Run Code Online (Sandbox Code Playgroud)

果然,我得到:

testit.h:6: error: conflicting declaration ‘typedef struct foobar foobar’
somethingelse.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’
Run Code Online (Sandbox Code Playgroud)

或者如果我在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’
Run Code Online (Sandbox Code Playgroud)

行号始终不同 - 声明不能与自身冲突.我不明白.谁有人见过这个?

Har*_*lby 15

单个标头是否包含在多个源文件中?如果是这样,你需要将它包装在"include guard"中,如下所示:

#ifndef TAUCS_H
#define TAUCS_H

//Header stuff here

#endif //TAUCS_H
Run Code Online (Sandbox Code Playgroud)

  • 但是,您应该避免使用前导双下划线.这些名字是保留的. (2认同)
  • 尾随双下划线也是如此.一个正确的形式是`TAUCS_H`.C和C++程序员不会将带有_H后缀的宏用于其他目的,因此这是安全的,无需进一步修饰. (2认同)

Tim*_*mwi 6

是否.../taucs/src/taucs.h包含声明的头文件()(直接或间接)由两个单独的#include指令包含两次?