在 C++ 中跨 类 使用结构

Using a struct across classes in c++

我对 C++ 还是很陌生,所以请允许! 本质上,我已经在 header 文件中为我的一个 类 充满字符串的文件创建了一个结构。

typedef struct details{
        string name;
        string address;
}details;

而且我不仅希望在属于 header 的 cpp 文件中使用此结构,而且在其他 类 中也使用此结构。例如,我想在另一个 header 文件中创建此结构的向量。

private:
vector <details> foo;

我也想用main中的struct

details d;
d.name = "hi";
d.address = "hello";

然而,当我目前尝试这样做时,我遇到了诸如

之类的错误
error: 'details' was not declared in this scope
     vector <details> foo;

error: template argument 1 is invalid
     vector <details> foo;

有没有人遇到过类似的问题,谁可以现场提供我可以做些什么来解决这个问题?非常感谢。

编辑以演示代码

class1.h

#include "class2.h"

struct trans1{
    string name;
};
class class1 {

private:
    vector <trans2> t2;

public:
    class1();
};

class2.h

#include "class1.h"

struct trans2{
    string type;
};

class class2{

private:
    vector <trans1> t1;

public:
    class2();
};

错误日志:

In file included from class1.h:3:0,
                 from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
     vector <trans1> t1;
             ^
class2.h:21:19: error: template argument 1 is invalid
     vector <trans1> t1;
                   ^
class2.h:21:19: error: template argument 2 is invalid

我知道这在现实世界的应用程序中是荒谬的代码,但这是我可以演示的最简单的方法

您必须在任何使用它的地方包含包含 struct 定义的头文件。

唯一不需要的情况是当您只声明对它的引用或指针时;在这种情况下,您可以使用以下方式转发声明:

struct details;

同样在 C++ 中,您可以通过以下方式声明它:

struct details{
    std::string name;
    std::string address;
};

typedef.

真的没必要

C++ 中的结构定义类似于

 #include <string>

 struct details{
    std::string name;
    std::string address;
 };

并且在您的代码中的其他地方使用它之前必须看到它。假设您将上面的声明放在头文件 details.hpp 中,您应该有以下内容来使用它

 #include <vector>
 #include "details.hpp"

 // Some context

 std::vector<details> vdetails;

在某些情况下,当 details 结构成员实际上未被访问时,您还可以使用前向声明 as

 struct details;

而不是包括完整的结构声明。这可以用来在进一步的声明中声明 details* 指针或 details& 引用,只要它们没有被取消引用。

details.h

#include <string>

#ifndef DETAILS_H
#define DETAILS_H

struct details {
  std::string name;
  std::string address;
};

#endif

details.cpp

#include "details.h"
//insert implementation here

other_header.cpp

#include "details.h"
#include <vector>

std::vector<details> main_use;
//whatever else here

这应该有效。

编辑

如果你想在另一个中使用它class:

my_class.h

#include "details.h"
#include <vector>

#ifndef MYCLASS_H
#define MYCLASS_H

class myClass {
std::vector<details> class_use;
//insert stuff here

};

#endif

编辑 2

我不太确定为什么要像这样在 classes 中定义结构——这有点令人困惑。这就是我会怎么做。请注意 #ifndef ... #define ... #endif 模式 非常重要 。包含包含他们自己的 headers 也是一个坏主意。我将按以下方式组织您的代码(如您所用):

trans.h

#ifndef TRANS_H 
#define TRANS_H

#include <string>
struct trans1 {
   std::string name;
};

struct trans2 {
   std::string type;
};

#endif

class1.h

 #ifndef CLASS1_H
 #define CLASS1_H

 #include "trans.h"
 #include <vector>

 class Class1 {
   public:
     Class1();
   private:
     std::vector<trans2> t2;
 };

 #endif

class2.h

 #ifndef CLASS2_H
 #define CLASS2_H

 #include "trans.h"
 #include <vector>

 class Class2 {
   public:
     Class2();
   private:
     std::vector<trans1> t1;
 };

 #endif

现在按照它的组织方式,您可以通过 #include "trans.h" 在主体中使用 trans 结构,同时消除循环包含。希望这对您有所帮助。

你会发现下面的main.cc现在编译没有错误了。

main.cc

#include<iostream>
#include "class1.h"
#include "class2.h"
#include "trans.h"

int main()
{
    std::cout << "Hello, World!" << std::endl;

    return 0;

}

埃里普