Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+;中的不同类中使用在一个类中定义的struct+;?_C++_Class_Struct - Fatal编程技术网

C++ 如何在C+;中的不同类中使用在一个类中定义的struct+;?

C++ 如何在C+;中的不同类中使用在一个类中定义的struct+;?,c++,class,struct,C++,Class,Struct,我在一个类中有这个结构,Class1: static struct count { int member1; int member2; int member3; } count1, count2, count3; 在Class1中,我将递增所有三个count变量,并在另一个文件中的Class2中再次使用它。我该怎么做?我需要在头文件中包含Class1吗?静态变量的作用域是有限的,但整个程序的生存期都是有限的,如果在Class1定义中声明了计数结构,那么即使包含了定义了C

我在一个类中有这个结构,
Class1

static struct count {
    int member1;
    int member2;
    int member3;
} count1, count2, count3;

Class1
中,我将递增所有三个
count
变量,并在另一个文件中的
Class2
中再次使用它。我该怎么做?我需要在头文件中包含
Class1
吗?

静态变量的作用域是有限的,但整个程序的生存期都是有限的,如果在Class1定义中声明了计数结构,那么即使包含了定义了Class2的文件,也无法访问Class2中的静态计数对象。
您必须在文件中设置全局计数结构,然后才能将该文件包含在Class2中访问它

您确定该文件已正确标记吗??通常的方法是使声明对其他类可见(是的,包括头文件,您需要在其中查看
count1、count2、count3
)。