简体   繁体   English

向量<double>作为类成员变量

[英]Vector<double> as class member variable

Just like I'm creating member variables for x , y , and theta , how would I create member variables for the type vector<double> in the constructor?就像我为xytheta创建成员变量一样,如何在构造函数中为vector<double>类型创建成员变量? What I'm doing below doesn't seem to work.我在下面所做的似乎不起作用。

Positioning::Positioning(double x, double y, double theta) {
    this->globalX = x;
    this->globalY = y;
    this->globalTheta = theta;
    
    vector<double> this->xs;
}

I have done #include <vector> and have std::vector<double> xs;我已经完成了#include <vector>并且有std::vector<double> xs; in the private section of Positioning.hppPositioning.hpp的私有部分

EDIT编辑

Header file:头文件:

namespace Position {
    class Positioning {
        private:
            double globalX;
            double globalY;
            double globalTheta;
            std::vector<double> xs;

        public:
            Positioning(double x, double y, double theta);
            ~Positioning();
            void updateGlobalPosition(double left_dist, double right_dist, double rear_dist);
            double getX();
            double getY();
            double getTheta();
    };
}

This works这有效

Positioning::Positioning(double x, double y, double theta) {
    this->globalX = x;
    this->globalY = y;
    this->globalTheta = theta;
}

All your class variables are created automatically, you don't have to do anything special to create them.你所有的类变量都是自动创建的,你不需要做任何特殊的事情来创建它们。

The code in the body of the constructor above is not creating anything, it's assigning values to your variables.在构造函数体上面的代码也没有创造任何东西,它是你的变量分配值。 Of course if you want to give a member variable a particular value, then you have to do something, but they get created automatically.当然,如果你想给一个成员变量一个特定的值,那么你必须做一些事情,但它们是自动创建的。

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

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