Having problem designing a constructor

Hello..
I am making a game called Ultimate Tic-Tac-Toe in which i have taken 2 classes.. 1 for small tictactoe abd other for ultimate one..

Class Ultimate {
int size;
Tictactoe** board;
Public:
Ultimate (int s)
{
Size=s;
Board*=new tictactoe [size];
For(int i=0;i<size;i++)
Board[i]=new tictactoe [size];
}
};

//AFTER THIS, I ASSIGNED A VALUE TO board[i][j] IN A NESTED LOOP BUT WHEN I COUT THE BOARD, ALL THE ADDRESSES WERE STORED IN IT AND NOT THE INTEGER VALUES.

I JUST WANT TO MAKE AN ULTIMATE BOARD CONSISTING OF SMALL BOARDS OF 1D POINTERS IN THAT BIG BOARD. IS MY CODE RIGHT?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

struct Tictactoe { int value = 0 ; /* ... */ };

class ultimate_board
{
    // vector: https://cal-linux.com/tutorials/vectors.html
    std::vector< std::vector<Tictactoe> > board ;

    // create a size x size board
    ultimate_board( std::size_t size ) : board( size, std::vector<Tictactoe>(size) )
    {
        // and assign values 1 onwards to the cells
        int n = 0 ;
        // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
        // auto: http://www.stroustrup.com/C++11FAQ.html#auto
        for( auto& row : board ) for( auto& cell : row ) cell = Tictactoe{ ++n } ;
    }

    std::size_t size() const { return board.size() ; }
};
Well of course if you cout the board it will print addresses because board is pointer. pointers contain address not the actual value. You need to put a * before a pointer to get the value stored in it. And in your case you haven't assigned any values to your board you only created the array. So event if you use cout << *pointerName than a garbage value will show up
Last edited on
Well of course if you cout the board it will print addresses because board is pointer.

It's not necessarily obvious since printing a char pointer does not behave like that.
It's not necessarily obvious since printing a char pointer does not behave like that.


He wanted integers to show up when he use cout. There are no chars involve here. So its kinda obvious. I know that char pointer work differently but in the current scenerio he was expecting a int. So char pointer is out of the scope of the current question
It's obvious for you. I just didn't want Muiz to feel bad if it wasn't obvious for him. Because to be honest, it's not obvious unless you have learned that that is how it works.
Okay sorry for being rude
Topic archived. No new replies allowed.