C++ 在头文件中声明类对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/633634/
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-27 16:24:53  来源:igfitidea点击:

Declaring class objects in a header file

c++objectheaderlinker

提问by Raugnar

Greetings everyone.

问候大家。

I seem to be snagging on a fundimental but I cant find the solution anywhere. Anywho, will go ahead and explain.

我似乎在根本问题上陷入困境,但我无法在任何地方找到解决方案。任何人,都会继续解释。

I have a program consisting of three files; main.ccp, add.h, add.cpp.

我有一个由三个文件组成的程序;main.ccp、add.h、add.cpp。

I declare the class 'SA' in add.h and have all my functions defined in add.cpp

我在 add.h 中声明了类“SA”,并在 add.cpp 中定义了我的所有函数

additional.h

附加.h

class SA {
    ...
public
    int x;
} Obj1, Obj2;

main.ccp

主文件

#include "additional.h" 

int main() {

    Obj1.x = 5;

    ...
}

This gives me a link error on compiling: error LNK2005: "class SA Obj1" (?Obj1@@3VSA@@A) already defined in main.obj

这给了我编译时的链接错误:error LNK2005: "class SA Obj1" (?Obj1@@3VSA@@A) already defined in main.obj

The only deffinition of the object occurs in add.h, and no where else. The program compiles just fine if declare the objects in the main and not the header:

对象的唯一定义出现在 add.h 中,其他地方没有。如果在 main 中声明对象而不是在头文件中,则程序编译得很好:

main.ccp

主文件

#include "additional.h" 

int main() {

    SA Obj1;
    Obj1.x = 5;

    ...
}

The issue is that I want to use the objects primarially within add.cpp, but still need to initialise several public values through main.cpp. Any words of wisdom?

问题是我想主要在 add.cpp 中使用对象,但仍然需要通过 main.cpp 初始化几个公共值。有什么智慧的话吗?

回答by MrValdez

Declare Obj1 and Obj2 in your .cpp instead of at .h

在 .cpp 而不是 .h 中声明 Obj1 和 Obj2

add.h

添加.h

class SA {
 ...
public
    int x;
};

main.cpp

主程序

#include "additional.h" 

SA Obj1, Obj2;

int main() {

 Obj1.x = 5;

 ...
}

If you want to declare Obj1 and Obj2 in your .h file, add extern in the .h file like so:

如果要在 .h 文件中声明 Obj1 和 Obj2,请在 .h 文件中添加 extern,如下所示:

extern SA Obj1, Obj2;

but you should declare the objects in a .cpp file in your project:

但您应该在项目的 .cpp 文件中声明对象:

main.cpp

主程序

SA Obj1, Obj2;

The reason for this is that everytime you include the .h file, you are declaring Obj1 and Obj2. So if you include the .h file two times, you will create two instance of Obj1 and Obj2. By adding the keyword extern, you are telling the compiler that you have already decalred the two variables somewhere in your project (preferably, in a .cpp file).

这样做的原因是每次包含 .h 文件时,您都在声明 Obj1 和 Obj2。因此,如果您两次包含 .h 文件,您将创建 Obj1 和 Obj2 的两个实例。通过添加关键字 extern,您告诉编译器您已经在项目中的某处(最好在 .cpp 文件中)对这两个变量进行了标记。

回答by sharptooth

Use externkeyword. Declare these public objects as extern in header, then define instances in one of the cpps.

使用extern关键字。在头文件中将这些公共对象声明为 extern,然后在其中一个 cpps 中定义实例。

Like this:

像这样:

extern SA Obj1; // in header

SA Obj1;// in any one (no more than one) cpp

回答by Ben

You can also have the following line of code in your header file:

您还可以在头文件中包含以下代码行:

static SA Obj1, Obj2;

This will create only 1 instance of each object at run time and prevent redefinitions. However, keep in mind that all files that include the .h file will be using the same instance of those two objects so you will need to be careful when you are accessing them.

这将在运行时仅创建每个对象的 1 个实例并防止重新定义。但是,请记住,包含 .h 文件的所有文件都将使用这两个对象的相同实例,因此在访问它们时需要小心。