简体   繁体   English

结构和嵌套结构的前向声明

[英]Forward declaration for struct and nested struct

Is any way to use forward declaration for struct X and Y which is nested as II need to use them in the hpp for some of the class members, but I would like to have them in the cpp because my hpp is included in many places Thank you so much for any help!有什么方法可以对嵌套的结构 X 和 Y 使用前向声明,因为我需要在 hpp 中为某些类成员使用它们,但我希望将它们放在 cpp 中,因为我的 hpp 包含在很多地方谢谢非常感谢您的帮助!

//F1.hpp
#ifndef F1_HPP_
#define F1_HPP_

struct X;

struct Y
{
  struct Y1
  {
    int y1;  
  };
  X x1;
};


class Y1
{
public:    
    void f(X x);
    void f2(Y::Y1 y1);
};

#endif  // F1_HPP_

//F1.cpp
#include "F1.hpp"
#include <iostream>

struct X 
{
    int x;
    int x2;
    int x3;
};


void Y1::f(X x)
{
    std::cout<<"-1-\n";
}

// main.cpp
#include <iostream>
#include "F1.hpp"
using namespace std;

int main()
{   
    X x;
    Y1 f1;
    f1.f(x);

    return 0;
}

Short Answer: No (as far as my c++ knowledge concern)简短回答:否(就我的 C++ 知识而言)

Why: First, if you want to declare a variable of any type you need to know the exact size of the type.原因:首先,如果要声明任何类型的变量,您需要知道该类型的确切大小。 That is why forward declaration is not enough BUT pointers.这就是为什么前向声明是不够的但指针。 Because size of pointers are always the same no matter the data type, you can use forward declaration for pointers.因为无论数据类型如何,指针的大小始终相同,因此您可以对指针使用前向声明。

Nested Types: As far as I know of C++ we can't nest types with just variables BUT pointers.嵌套类型:据我所知,C++ 我们不能只使用变量但指针嵌套类型。 You can use pointers and forward declaration to nest types.您可以使用指针和前向声明来嵌套类型。

Maybe something like this:也许是这样的:

struct Y
{
  struct Y1
  {
    int y1;  
  };
  X* x1;
};


class Y1
{
public:    
    void f(X* x);
    void f2(Y::Y1* y1);
};

We cannot have a nonstatic data member of incomplete type.我们不能有不完整类型的非静态数据成员。 In particular, with only a forward declaration for X , we cannot define a data member of type X as you've done inside F1.hpp .特别是,只有X的前向声明,我们不能像您在F1.hpp中所做的那样定义X类型的数据成员。 This can be seen from type :这可以从type中看出:

Any of the following contexts requires type T to be complete:以下任何上下文都要求类型T是完整的:

  • declaration of a non-static class data member of type T ;声明类型为T的非静态类数据成员

Better would be to create separate header and source file for each class and then include the header wherever needed/required as shown below :更好的是为每个类创建单独的头文件和源文件,然后在需要/需要的地方包含头文件,如下所示

Yfile.h文件.h

#ifndef F1_HPP_
#define F1_HPP_

#include "Xfile.h" //needed for X x1; data member
struct Y
{
  struct Y1
  {
    int y1 = 0;  
  };
  X x1;
};
#endif

Xfile.h Xfile.h

#ifndef X_H
#define X_H

#include <iostream>

struct X 
{
    int x = 0;
    int x2 = 0;
    int x3 = 0;
};

#endif

Y1file.h Y1文件.h

#ifndef Y1_H
#define Y1_H
#include "Yfile.h"
//forward declarations for parameters 
struct X; 


class Y1
{
public:    
    void f(X x);
    void f2(Y::Y1 y1);
};

#endif

Y1file.cpp Y1文件.cpp

#include "Y1file.h"
#include "Xfile.h"
#include "Yfile.h"
void Y1::f(X x)
{
    std::cout<<"-1-\n";
}
void Y1::f2(Y::Y1 y1)
{
    std::cout<<"f2"<<std::endl;
}

main.cpp主文件

#include "Xfile.h"
#include "Y1file.h"
int main()
{   
    X x;
    Y1 f1;
    f1.f(x);

    return 0;
}

Working demo工作演示

The output of the above program is:上述程序的输出是:

-1-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM