【问题标题】:Creating a vector of vectors to be used in a class and plotted using CERNROOT创建要在类中使用并使用 CERNROOT 绘制的向量向量
【发布时间】:2020-10-06 10:31:05
【问题描述】:

我是编程新手,希望创建一个类或头文件(老实说,我不确定我说的是什么,初学者物理学家,试图利用我的空闲时间,但我认为这是一个头文件看到即将提到的对象的定义将在其中)。 我在创建向量向量时遇到问题。我认为它是这样完成的:

    #include <array>
    #include <vector>


    constexpr int MaxIterations = 101;
    constexpr double aprox_PI = 3.141592653589793238463;

    using PiArray = std::array<double, MaxIterations>;
    using PiMatrix = std::array<PiArray, MaxIterations>;
    using PiVector = std::vector<std::vector<double>> ; //here lies the problem, the ones above work fine.

    class MonteCarloPiCalculator{
        //some stuff goes in here, like function declarations. The definitions are in another file
    }

所以vector的大小和内容可以在后面添加,也可以提前定义。这就是我使用的绘图工具的方式。

auto* gr4 = new TGraph(calc.VectPot.data(), calc.relDiff.data());

从 ROOT TGraph 文件中我发现了这个:

TGraph(const TVectorF &vx, const TVectorF &vy); 
TGraph(const TVectorD &vx, const TVectorD &vy);

这意味着我可以使用带有浮点和双精度值的向量。 甚至在编译之前,我在 Visual Studio 中就收到了这条消息:

没有构造函数实例与参数列表参数类型匹配 是: (const std::vector> *, const std::vector> *)

我将如何解决这个问题?我会很感激我能得到的任何帮助。 提前感谢您的宝贵时间。

【问题讨论】:

  • 第一条评论:通常在 C++ 中,我们尽可能避免使用原始指针,而支持智能指针:shared_ptr、weak_ptr 和 unique_ptr。
  • 另外,为了解决您的问题,最好有stackoverflow.com/help/minimal-reproducible-example
  • 错误只是说明您正在尝试使用错误类型的参数构造对象。

标签: c++ class vector root-framework


【解决方案1】:

TGraph(const TVectorD &vx, const TVectorD &vy);

这些是对 TVectorD 的引用,而不是原始指针。

【讨论】:

  • 是的,这也是我在阅读错误后的想法。您是否知道如何修改向量定义的向量以在这种情况下工作?另外感谢有关可重现示例的提示,我会尝试做一些事情!
  • 我已经解决了这个问题。我只是使用我的数组格式而不是矢量格式。感谢您的帮助。
  • 基本上,您需要将参数与构造函数以及构造函数中定义的内容相匹配。如果TVectorD 是std::array,则需要使用std::array 或为std::vector 添加构造函数。不看你的代码很难说更多。
  • 再次感谢您!昨天看到您的评论后,我使用了数组而不是向量。它现在按预期工作。
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
相关资源
最近更新 更多