-2

i saw a few posts with same problem but i didn't not manage to understand how do i make the temporary object to constant

Map.H

#ifndef _Map_
#define _Map_
class Map
{
private:

    int Board[7][7];

public:

    Map();
    Map(int mapNum);
    ~Map();
    void print() const;
};

#endif

Map.Cpp basicly just creates a 7*7 array with 0 or 1 in all places

Robots.H

#ifndef _Robot_
#define _Robot_
#include "Map.h"
class Robot
{
private:
    int _RobotID;
    int _mapNum;
    int _X;
    int _Y;


public:
    Robot();
    ~Robot();
    Robot (int mapNum, int Line, int Column);
    void setRobotID(int newid);
    void print() const;
};

#endif

s Robot.cpp

#include "Robot.h"
#include "Map.h"
#include <iostream>
#include "Game.h"
using namespace std;


    Robot::Robot()
    {
    }

    Robot::Robot(int mapNum, int line, int column) {
        _mapNum = mapNum;
        _X = line;
        _Y = column;
        _RobotID=0;

    }

now creating a map in my main works and so does printing it. same goes for robot. what i want to do is connect the robot and the map inside my "game.cpp\game.h" so that each robot that i add will check in the map (double array with 0's or 1's )if it has a 1 it wont add it to map. and if it has a 0 it will. (addRobot function is suppose to do that)

Game.H

#ifndef _Game_
#define _Game_
#include <vector>
#include <iostream>
#include "Map.h"
#include "Robot.h"
class Game

{
private:
    static int _RobotsNum;
    Map map1;
    Map map2;


public:
    void AddRobot(int mapnum, int x, int y);
    Map getMap(int mapnum);
    Game();
    ~Game();

};
#endif

Game cpp

#include "Game.h"
#include <algorithm>
#include <vector>

using namespace std;

int Game::_RobotsNum = 0;
vector <Robot> RobotVec;
Game::Game()
    : map1(1),
    map2(2)
{
}
Game::~Game()
{
}
void Game::AddRobot(int mapnum, int x, int y) {

my main

    int main() {
    Game game;
//  Game* pgame = new Game();
    game.AddRobot(1, 3, 4);
    game.AddRobot(1, 4, 4);
    game.AddRobot(1, 5, 4);

hope you guys can help me. thanks

Dino Cell
  • 53
  • 8
  • Also recommended read: https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr – user0042 Dec 30 '17 at 13:20
  • This isn't the problem, but names that begin with an underscore followed by a capital letter (`_Robot_`, `_Game_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Dec 30 '17 at 13:25
  • *"it calls the destroyer"* Of what? Which instance of which class do you believe is improperly destroyed? – Igor Tandetnik Dec 30 '17 at 13:27
  • when i debug the main. for example in the last line, with the add robot. i see in the debugger then in class AddRobot i do get a new robot with all the valus i want. but once i goes back to main. i get that pgame is empty – Dino Cell Dec 30 '17 at 13:34

2 Answers2

2

This constructor has three local variables with the same names as other variables:

Game::Game()
{
    vector <Robot> RobotVec; // Not your global variable
    Map map1(1);             // Not your member variable
    Map map2(2);             // Not your member variable either
}

In order to initialise members, you use the initialiser list:

Game::Game()
  : map1(1),
    map2(2)
{
}

In addRobot, this creates a robot and points X at it:

Robot* X = new Robot;

This also creates a robot, so now you have two:

Robot newRobot(mapnum, x, y);

And this memory leak points X away from its original robot and instead points it at newRobot, which will be destroyed immediately afterwards:

X = &newRobot;

Note that addRobot does not at any point add either robot to anything – it creates two and ignores them both.

You should make the vector a member (avoid global variables unless repeating other people's mistakes is a particular passion of yours):

class Game
{
private:
    int robotsNum;
    vector<Robot> robotVec;
    Map map1;
    Map map2;
// ...
};

Game::Game()
  : robotsNum(0),
    map1(1),
    map2(2)
{
}

And add your new robot to the vector:

void Game::AddRobot(int mapnum, int x, int y) {
    // ...

    Robot newRobot(mapnum, x, y);
    robotsNum++;
    newRobot.setRobotID(robotsNum);
    robotVec.push_back(newRobot);
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • thanks for the help but i get an "error : missing type specifier - int assumed" , but if i move "vector RobotVec" to game.cpp as global varibel it does work... (but you said its not suggested) – Dino Cell Dec 30 '17 at 23:31
  • i edited my post so you can see the changes – Dino Cell Dec 30 '17 at 23:43
0
Robot newRobot(mapnum, x, y);

This creates an object of type Robot named newRobot. At the end of the block where it was created it will be destroyed.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • but i want to use the AddRobot function to add a new robot. but once i run it. it gets destroyed right after it is created : pgame->AddRobot(1, 1, 1); it just creates and removes in same line – Dino Cell Dec 30 '17 at 13:32