简体   繁体   中英

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? 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; in the private section of Positioning.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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