Search code examples
c++classconstructorvariable-assignmentfriend

array from friend class in assignment operation is being manipulated


I have a class called Map with the following declaration:

class Map {
public:
    void get_world_size(const double val);
    void get_landmarks(const double array[][2], int val);
    void print_map();

private:
    int num_landmarks;
    double world_size;
    double landmarks[][2];

    friend class Robot;
};

where I have defined Robot as a friend class. In order to avoid having an explicit constructor in Robot objects, I am copying the private attributes to the Robot class as

void Robot::get_world_size(const Map map){
    this->world_size = map.world_size;
}

void Robot::get_landmarks(const Map map){
    this->num_landmarks = map.num_landmarks;
    for(int i = 0; i < this->num_landmarks; i++){
        if (i == 0) {
            std::cout << "The map landmarks are being copied to robot landmarks..." << std::endl;
        }
        this->landmarks[i][0] = map.landmarks[i][0];
        this->landmarks[i][1] = map.landmarks[i][1];
        std::cout << " Landmark " << i << " = " << map.landmarks[i][0] << "\t" << map.landmarks[i][1] << " is copied!" << std::endl;
    }
}

There are at least two issues I can't fully understand: (1) in the Robot class, the attribute double landmarks[][2] throws:

In copy constructor ‘constexpr Robot::Robot(const Robot&)’:
.../Robot.h:41:12: error: initializer for flexible array member ‘double Robot::landmarks [][2]’
   41 |     double landmarks[][2];

And even if I very naively define it as double landmarks[8][2] to get rid of this error, the content of assignment is wrong, namely for

Map map;
    // Map size in meters
    int map_size = 100;
    map.get_world_size(map_size);
    int num_landmarks = 8;
    double landmarks[8][2] = { { 20.0, 20.0 }, { 20.0, 80.0 }, { 20.0, 50.0 },
                               { 50.0, 20.0 }, { 50.0, 80.0 }, { 80.0, 80.0 },
                               { 80.0, 20.0 }, { 80.0, 50.0 } };

    map.get_landmarks(landmarks, num_landmarks);
    map.print_map();

    //Practice Interfacing with Robot Class
    Robot myrobot;
    myrobot.get_world_size(map);
    myrobot.get_landmarks(map);
    myrobot.print_robot();

I am getting very strange result in print_robot method:

20  20
20  80
20  50
50  20
50  80
80  80
80  20
80  50
Robot indicates the world size as: 100
The map landmarks are being copied to robot landmarks...
 Landmark 0 = 0 1.6976e-313 is copied!
 Landmark 1 = 6.91261e-310  100 is copied!
 Landmark 2 = 20    20 is copied!
 Landmark 3 = 20    80 is copied!
 Landmark 4 = 20    50 is copied!
 Landmark 5 = 50    20 is copied!
 Landmark 6 = 50    80 is copied!
 Landmark 7 = 80    80 is copied!

Can you please help me?

Edit: The issue was as mentioned in replies the wrong initialization.


Solution

  • double landmarks[][2]; is not legal C++. Such declaration allowed only if you initialize it directly and then omitted size would be defined by size of the initializer list.