1

1 つ以上の C++ ファイルで見られる構造をグローバルに (extern struct を使用して) 宣言する方法について、やや詳細なスレッド (228684) がありましたが、その方法を正確に理解することはできません (多くの議論がありました)。これを行う、あれを行う、おそらくこれを行う、これを試すなど...)。

誰かが 2 つの別々の C++ ファイルで見られる構造を宣言する方法の非常に簡単な例を投稿できますか? すべての関数をメインと同じファイルに入れると正常に動作しますが、関数を別のファイルに分割しようとすると、コンパイルできません。

不明な点... 構造体を型定義する必要がありますか? ヘッダー ファイルで構造を定義し、そのヘッダーを各 C++ ソース ファイルに含めますか? ヘッダー ファイルに #ifndef マクロが必要ですか? 構造体 extern をヘッダーで宣言する必要がありますか?

4

4 に答える 4

7

It's called a header file.

in your header file (call it foo.h)

#ifndef FOO_H
#define FOO_H
class X {
};
#endif

Then, in any C files you have

#include "foo.h"
X x;

For C++ it's more common/preferred to use class, but you can use struct as well. The extern keyword generally refers to variables, not class/struct declarations. You would make a global variable extern in a header file (then declare it not-extern) in one of your .cpp files.

于 2008-11-25T19:21:31.097 に答える
3

Structures cannot be extern or static. If you want to have a structure used in more than two translation units, put the structure definition into a header:

foo.hpp

struct foo {
    int a;
    int b;
};

Then include that header file into all source files using that struct. Having a struct/class/union defined in multiple source-files is perfectly valid, as long as each one is same defined. You can put a Include Guard around the definition of foo to prevent that foo is included twice into the same sourcefile being compiled. (So, having multiple foo in the same program is valid, but having multiple foo in the same source (note: meaning translation unit) is not valid.) See 3.2 One Definition Rule in the C++ Standard or a Draft.

When you see this:

extern struct foo { int a; int b; } b;

Not the foo is extern, but the b (the object) is extern! It's saying that b is not defined, but merely declared, so you can refer to it.

于 2008-11-25T19:24:37.190 に答える
2

グローバル変数が必要な場合。複数のコンパイル単位 (C++ ファイル) からアクセスできる単一の構造体:

/* foo.h */
#ifndef EXAMPLE_FOO_H
#define EXAMPLE_FOO_H

struct foo {
    int a;
    int b;
};

extern struct foo globalFoo;

#endif /* EXAMPLE_FOO_H */

--

/* foo.cpp */
#include "foo.h"

struct foo globalFoo = { 1, 2 };

--

/* bar1.cpp */
#include "foo.h"

int test1()
{
    int c = globalFoo.b; //c is 2
}

--

/* bar2.cpp */
#include "foo.h"

int test2()
{
    int x = globalFoo.a; //x is 1
}

foo.h の「struct {}」行は、構造体 foo がどのように見えるかをコンパイラに伝えます。「extern struct」行は、外部リンケージを持つ「globalFoo」と呼ばれる特定の構造体 foo を宣言します。(これらの 2 つの宣言を組み合わせることができます。@litb の回答を参照してください。) これが意味することは、globalFoo (bar1/test1 および bar2/test2) のユーザーが構造体を他の場所で探すことであり、独自の個別のコピーはありません。

foo.cpp は、globalFoo を定義する特殊なケースです。任意の変数の定義は 1 つしか存在できません。この場合、foo.cpp には globalFoo の定義があります。bar1 と bar2 が「外部」で globalFoo を探すと、foo.cpp の globalFoo が見つかります。この名前は、3 つのファイルすべてで同じ実際の構造体を参照します。

そこになかった場合、extern3 つの .cpp ファイルすべてに「struct foo globalFoo;」という行が含まれます。(#include は単なるコピー/貼り付けであることを忘れないでください) それは、同じ名前の 3 つの異なる構造体を作成 [しよう] し、混乱を招きます。

注: " extern struct foo globalFoo;" 行は foo.cpp の場合は冗長ですが、それは問題ではありません。

于 2008-11-25T23:35:29.977 に答える
0

Here's a little example:

#ifndef __my_header__
#define __my_header__

class my_class
{

};

#endif

The class my_class will be visible in any file that includes this header. I think there is something else to your question, though, but I don't quite see what.

于 2008-11-25T19:22:51.500 に答える