【问题标题】:How to pass a vector struct from one header file to class in another header file如何将一个头文件中的向量结构传递给另一个头文件中的类
【发布时间】:2019-05-18 18:26:54
【问题描述】:

我有一个向量结构,我试图在另一个头文件中通过引用来调用向量。

头文件1

struct struct1
{
    struct1();

};
class  class1
{
public:
    std::vector<struct1> vector1;
}

其他头文件


class  class2
{
Public:
    class2();
    void function1(std::vector<struct1>& _vector);
}

在主 cpp 文件中

int main()
{

    class2.function1(class1::vector1);

    return 0;
}

头文件相互包含在主 ccp 文件中。 我得到的主要错误是“void function1(std::vector& _vector)”行

Error   C2903   'allocator': symbol is neither a class template nor a function template 

Error   C3203   'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type


如何让它正常工作?

【问题讨论】:

  • 您需要class1 的实例才能访问vector1 类成员,除非您将class1::vector1 设为static 类成员。
  • 在没有vector 的情况下让它工作。使用简单的int。这将向您展示潜在的问题。先解决这些。
  • vector1 是类class1 的实例成员。在您拥有class1 的实例之前,那里没有there。当然,您可以将vector1 静态化为class1 类(然后修复链接器错误,因为您忘记在某处实际定义静态实例,您无疑会立即得到),这可能/会“工作”。但它也很可能不是您想要的架构。 class2::function1 也有类似的问题,顺便说一句,这次是使用成员函数而不是成员变量。或许查看您的 C++ 文本。
  • 值得一提的是,这段代码的问题与标题和文本所暗示的多个头文件完全无关。即使这段代码都在同一个 cpp 文件中,除了库存系统头文件之外没有其他头文件,它也会失败。
  • 这里错得太多了,我会多研究 C++ 的基础知识。您需要了解类的类型和实例(AKA 对象)之间的区别。

标签: c++


【解决方案1】:

如何让它正常工作?

根据我可以推断出您显示的代码,您需要在 main() 中执行以下操作:

int main() {
    class2 c2; // Avoid naming variables the same as their typenames
    class1 c1;
    c2.function1(c1.vector1);

    return 0;
}

详细说明:

  • 头文件包含class/struct接口的声明1
  • 除非这些可公开访问的 class / struct 成员被声明为 static,否则您将需要一个实例来访问它们。

1)请注意,classstruct 定义需要在最后一个右大括号 (}) 后用分号 (;) 关闭

【讨论】:

    【解决方案2】:

    让我向您展示最简单的方法(online editor):

    head1.cpp

    #include <vector>
    
    struct struct1
    {
        struct1();
    };
    class  class1
    {
    public:
        std::vector<struct1> vector1;
    };
    

    head2.cpp

    #include <vector>
    #include "head1.cpp"
    
    class  class2
    {
    public:
        class2() {};
        void function1(std::vector<struct1>& _vector) {};
    };
    

    main.cpp

    #include <iostream>
    #include "head2.cpp" // Only head2 is needed as head1 is already imported in head2
    
    using namespace std;
    
    int main() {
        class2 c2;
        class1 c1;
        c2.function1(c1.vector1);
    
        return 0;
    }
    
    

    【讨论】:

      【解决方案3】:

      编译并正常工作:

      ma​​in.cpp

      #include "class1.h"
      #include "class2.h"
      #include <vector>
      
      int main()
      {
          class1 first;
          class2 second;
      
          second.function1(first.vector1);
      
          return 0;
      }
      

      class1.h

      #pragma once
      #include "struct1.h"
      #include <vector>
      
      class class1
      {
      public:
          std::vector<struct1> vector1;
      };
      

      class2.h

      #pragma once
      #include "struct1.h"
      #include <vector>
      
      class class2
      {
      public:
          class2()
          {
      
          }
      
          void function1(std::vector<struct1>& _vector)
          {
      
          }
      };
      

      struct1.h

      #pragma once
      
      struct struct1
      {
          struct1()
          {
      
          }
      };
      

      【讨论】:

        【解决方案4】:

        您需要使用 struct1 创建一个头文件,并将其包含在您的“头文件 1”和“其他头文件”中.

        我不确定,但它可能会起作用:只需在 class2 声明之前的“其他头文件”中声明您的 struct1像这样:struct struct1;

        【讨论】:

        • struct1main() 中出现的问题完全无关。
        猜你喜欢
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 2011-07-17
        • 2022-06-10
        • 1970-01-01
        • 2010-09-06
        • 2015-05-22
        • 1970-01-01
        相关资源
        最近更新 更多