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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3553265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:06:10  来源:igfitidea点击:

How can a declaration conflict with itself?

c++g++solver

提问by eeeeaaii

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

这是我在尝试编译一些使用 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'

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:

在文件 testit.h 中:

#include "somethingelse.h"

typedef struct
{
  int n;
} foobar;

In file somethingelse.h:

在文件 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:

或者如果我在 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?

行号总是不同的——声明不能与自身冲突。我不明白。有人见过这个吗?

采纳答案by Timwi

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

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

回答by Harper Shelby

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

回答by David Feurle

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.

在这个例子中,你给出了 2 个 foobar 声明。编译器不知道选择哪一个 - 所以它会因声明冲突而退出。你不能两次声明同样的事情。

回答by R Samuel Klatchko

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

不要重复定义。C++ 允许一个定义只出现一次。您可以做的是重复声明。

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

Atypedef始终是一个定义。所以我建议的第一件事是给一个struct正确的名字(因为这是 C++,typedef 不会增加任何好处,所以只需删除 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):

接下来,它应该在一个文件中。如果您的文件只使用指向 foobar 的指针,则可以重复声明(不是定义):

// file2.h

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

void doit(const foobar *f); 

回答by Thomas H?henleitner

I had the same issue linting my code and it was notdouble 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 andC++ files. Hope that helps someone.

我在整理我的代码时遇到了同样的问题,它不是类型的双重声明。PC-Lint 抱怨在混合 C 和 C++ 代码中使用了相同的 typedef。我可以通过避免在 CC++ 文件中使用相同的声明来解决这个问题。希望能帮助某人。