从另一个类中修改对象的std :: vector

Jor*_*eno 2 c++ pass-by-reference

介绍:

在我的程序中,一个group对象有一个std::vector完整的多边形(一个环和一个孔),一个polygon对象有一个std::vector点.实际上,课程更复杂,我把它们剥离在这里以说明我的问题.

问题:

我希望能够point从它的相应组对象中修改'sx和y坐标.下面,group.cpp我有一个虚拟函数void Group::tryChangingAllX(),试图完成这个.但是,之后调用show()该组显示它的多边形点的坐标没有变化.

我想我需要使用引用/指针,但我需要在正确的方向上轻推.

point.cpp:

#include "point.h"
#include <iostream>
Point::~Point(){}
Point::Point(int x, int y){
    _x = x;
    _y = y;
}
void Point::show(){std::cout << "(" << x() << "," << y() << ")";}
void Point::x(int x){_x = x;}
void Point::y(int y){_y = y;}
int Point::x(){return _x;}
int Point::y(){return _y;}
Run Code Online (Sandbox Code Playgroud)

point.h:

#ifndef POINT_GUARD
#define POINT_GUARD
class Point{
    int _x;
    int _y;
    public:
        Point(int x, int y);
        ~Point();
        void show();
        int x();
        int y();
        void x(int x);
        void y(int y);  
};
#endif
Run Code Online (Sandbox Code Playgroud)

polygon.cpp:

#include "polygon.h"
#include "point.h"
#include <iostream>
#include <vector>

Polygon::~Polygon(){}
Polygon::Polygon(){}
std::vector<Point> Polygon::points(){return _points;}
Polygon::Polygon(std::vector<Point> points){_points = points;}
void Polygon::show(){
    std::cout << "Points: ";
    for(std::vector<Point>::size_type i = 0; i != _points.size(); i++) {
        _points[i].show();
    }
}
Run Code Online (Sandbox Code Playgroud)

polygon.h:

#ifndef POLYGON_GUARD
#define POLYGON_GUARD

#include <vector>
#include "point.h"

class Polygon{
    //private:
    std::vector<Point> _points;
    public:
        ~Polygon();
        Polygon ();
        Polygon(std::vector<Point> points);
        std::vector<Point> points();
        void show();
};
#endif
Run Code Online (Sandbox Code Playgroud)

group.cpp:

#include <iostream>
#include <vector>
#include "group.h"
#include "polygon.h"
Group::~Group(){}
Group::Group(std::vector<Polygon> polygons){
    _ring = polygons.front();
    polygons.erase(polygons.begin());
    _holes = polygons;
}
void Group::tryChangingAllX(){
    std::vector<Point> points = _ring.points();
    for(std::vector<Point>::size_type i = 0; i != points.size(); i++) {
        points[i].x(15);
    }
}
void Group::show(){
    _ring.show();
    if(_holes.size()>0){
        for(std::vector<Polygon>::size_type i = 0; i != _holes.size(); i++) {
            _holes[i].show();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

group.h:

#ifndef GROUP_GUARD
#define GROUP_GUARD

#include <vector>
#include "polygon.h"

class Group{
    Polygon _ring;
    std::vector<Polygon> _holes;
    public:
        ~Group();
        Group(std::vector<Polygon> polygons);
        void show();
        void tryChangingAllX();

};
#endif
Run Code Online (Sandbox Code Playgroud)

谢谢!

Luc*_*ore 5

功能

std::vector<Point> points();
Run Code Online (Sandbox Code Playgroud)

按值返回,因此当您调用它时,您将获得该成员的副本.您需要将其更改为

std::vector<Point>& points();
Run Code Online (Sandbox Code Playgroud)

你做完这个之后

std::vector<Point> points = _ring.points();
Run Code Online (Sandbox Code Playgroud)

还会复制返回的值.要引用实际成员_ring,请更改为:

std::vector<Point>& points = _ring.points();
Run Code Online (Sandbox Code Playgroud)

应该这样做.

请注意,您应该通过std::vectorconst引用来防止不必要的副本:

Polygon(const std::vector<Point>& points); 
Run Code Online (Sandbox Code Playgroud)

并考虑制作不修改类的方法const:

int x() const;  
Run Code Online (Sandbox Code Playgroud)