Thread: Pointer and memory allocation problem

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Question Pointer and memory allocation problem

    Alright, I'm making an object oriented Battleship game, there are 2 classes, 'Ship' and 'Board'

    The board is the ocean that the ships are placed in, and there are 2 objects of this class for every game that is played.

    The class contains a member variable m_oceanArray of type short int.

    Code:
    short int *m_oceanArray;
    The classes constructor then trys to initalize this pointer to a size of [x] and [y]. X and Y are two values passed to the constructor, based on the users input.

    Code:
    m_oceanArray = new short int[x][y];
    I'm probably doing something horribly wrong, but I don't know what! Logic suggests I have to use something like realloc() to do this, but I'm unsure..

    Thanks in advance.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Theres some code here that will show you what you need to do.
    This thread
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    bool **m_shipsDamage;
    short int **m_shipsLength;

    Code:
    Ship::Ship(short int length)
    {
    
    	m_shipsDamage = new bool * [2];
    	m_shipsLength = new short int * [2];
    
    	for (short int i = 0; i < 2; i++)
    	{
    		m_shipsDamage[i] = new bool[length];
    > 		m_shipsLength[i] = new short int * [2];
    	}
    };


    > BattleshipV2.cpp(182) : error C2440: '=' : cannot convert from 'short ** ' to 'short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast.


    I have no idea what this is.. nor how to fix it.. I got a few other errors when making a refrence to the matrix, but I fixed it by sticking a * infront.
    Last edited by Dual-Catfish; 02-17-2002 at 11:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM