【问题标题】:How do I feed a function containing a vector with different data types with these different data types?如何为包含具有不同数据类型的向量的函数提供这些不同数据类型的函数?
【发布时间】:2021-05-26 13:20:25
【问题描述】:

我仍在努力实现自己想要做的事情。 我的代码将采用用户定义的线段(例如,一条线、一个圆或我将实现的任何几何线段定义)并将它们链接在一起形成一个向量。但是,段类型的顺序(“line”、“circle”、...)是用户定义的,因此可能因执行而异。

在我继续之前:每个段都有自己创建所需的不同输入数据(例如,一条线没有半径,只有起点和终点)。

我的首选方法是

  1. 读取用户输入并识别分段顺序
  2. 创建每个细分
  3. 将这些提供给函数(例如,实现轮廓的类的成员函数/方法)。
  4. 此函数创建轮廓,例如通过实现向量。

我当前的测试代码有一个硬编码的段序列,但我想要实现的技巧是段的顺序(和数量)不是硬编码的。不幸的是,我不知道怎么做。

代码如下:

#include <iostream>
#include <vector>

struct point
{
    double x;
    double y;
};

class segment
{
public:
    segment() 
    {
        P1.x = 0;
        P1.y = 0;

        P2.x = 0;
        P2.y = 0;
    };
    virtual ~segment() {};

    virtual double get_radius() { return 0; };
    virtual double get_length() { return 0; };
    virtual double get_angle() { return 0; };

    int segment_id = 0;

protected:
    point P1;
    point P2;
};

class Line : public segment
{
public:
    Line() {};
    Line(const point pt1, const point pt2)
    {
        P1.x = pt1.x;
        P1.y = pt1.y;

        P2.x = pt2.x;
        P2.y = pt2.y;

        segment_id = 1;
    };

    ~Line() {};

    double get_length() { return calc_length(); };
    double get_angle() { return calc_angle(); };

private:
    double calc_length()
    {
        // calculate length (here: dummy value)
        return 1;
    }

    double calc_angle()
    {
        // calculate angle (here: dummy value)
        return 0.5;
    }

    double length = 0;
    double angle = 0;
}
;

class circle : public segment
{
public:
    circle()
    {
        center.x = 0;
        center.y = 0;
    };
    circle(const double r, const point c)
    {
        radius = r;
        center.x = c.x;
        center.y = c.y;

        segment_id = 2;
    };

    ~circle() {};

    double get_radius() { return radius; };
    point get_center() { return center; };
    double get_length() { return 3.14 * radius; }; //returns circumference

private:
    double radius = 0;
    point center;
};

//-------------------------------------------------------

int main()
{
    int nbr = 5;
    point start;
    start.x = 1;
    start.y = 2;

    point end;
    end.x = 3;
    end.y = 4;

    point c;
    c.x = 0;
    c.y = 0;

    double r = 9;

    auto anotherCircle = std::make_unique<circle>(r, c);
    auto anotherLine = std::make_unique<Line>(start, end);

    std::unique_ptr<circle> yet_anotherCircle;

    circle* myCircle = new circle(r, c);
    Line* myLine = new Line(start, end);
    
    //VERSION 1: Does not compile. I get an exception in <memory> line 1762 when trying to delete _Ptr
    //std::vector<std::unique_ptr<segment>> v1;
    //v1.emplace_back(anotherCircle);
    //v1.emplace_back(anotherLine);
    //std::cout << v1[0]->get_radius() << std::endl;
    //v1.emplace_back(myLine);
    //std::cout << v1[1]->segment_id << std::endl;

    //VERSION 2: Compiles
    std::vector<std::unique_ptr<segment>> v2;

    v2.emplace_back(std::make_unique<circle>(r, c));

    v2.emplace_back(std::make_unique<Line>(start, end));

}

我想象但似乎行不通的直接方式需要版本 1 才能工作。然后我可能会使用我输入向量的模板对象。不幸的是,这不是要走的路,我一点也不知道如何解决这个问题。如果有人可以在这里帮助我,那就太棒了!谢谢!

【问题讨论】:

    标签: c++ pointers vector


    【解决方案1】:

    您需要移动向量中的项目,因为您的项目不可复制:

    v1.emplace_back(std::move(anotherCircle));
    

    【讨论】:

    • 哇!这样可行!谢谢!我在这一点上挣扎了两天,你在不到五分钟的时间内为我解决了这个问题。不幸的是,我只能投票一次。
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2020-01-23
    • 2011-06-27
    • 2022-11-29
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多