|
-
Apr 18th, 2002, 12:47 PM
#1
More Problems from Nomad!
Now I'm getting an access violation. The program compiles and runs but will quit and ask me if I want to debug. When in the debugger it says I have an access violation in lattice.
Lattice is a double pointer array thingy. Declared like so:
char * * lattice;
Okay, now I want to get the value of a single 'cell' so I go use lattice[x][y], this sound good so far? It should return a char type correct? To make it more complicated lattice is in the inherited template class (iTClass1), and I tried using lattice in the template class that inherits iTClass1 (TClass2). I've tried getting lattice in a public funtion of TClass2, a protected function of TClass2, even tried returning a char type from a public function of iTClass1 to TClass2.
I can post code, but I think it would be more complicated. Also, if it matters TClass2 inherits iTClass1 like this:
class TClass2 : public iTClass1<data>
What can I do? Why is it doing this?
NOMAD
MS VC++ 6.0
Win ME
AMD 700
-
Apr 18th, 2002, 04:12 PM
#2
Are you sure you are initializing the array correctly?
Post the code.
Z.
-
Apr 18th, 2002, 05:43 PM
#3
See bottom for attachment of all files.
PHP Code:
#ifndef MAZE_H
#define MAZE_H
#include <iostream>
//using std::cout;
//using std::endl;
#include <stdlib.h>
#include <ctime>
#include "grid.h"
#include "screen.h"
using namespace std;
template <class data>
class maze : public grid<data>
{
protected:
unsigned short entranceX;
unsigned short entranceY;
unsigned short exitX;
unsigned short exitY;
unsigned short actualheight;
unsigned short actualwidth;
data blockedSymbol;
data unblockedSymbol;
//data getLattice(unsigned short x, unsigned y);
public:
maze();
maze(unsigned short width, unsigned short height,
unsigned int blockedPercent, data blockSymbol,
data noblockedSymbol);
maze(maze & m);
bool setEntrance(unsigned short e);
bool setExit(unsigned short e);
//bool randomBlocks(unsigned short numblocks);
bool IsThisCellBlocked(unsigned int x, unsigned int y);
bool IsThisCellTheEntrance(unsigned int x, unsigned int y);
bool IsThisCellTheExit(unsigned int x, unsigned int y);
void getEntrance(unsigned short &x, unsigned short &y);
// friend ostream & operator << (ostream & o, const maze<data> m);
// friend ostream & operator << (ostream & o, const maze<data> m);
};
//END CLASS DECLARATION
template <class data>
maze<data>::maze():grid<data>(unblockedSymbol, 10, 10)
{
}
template <class data>
maze<data>::maze(unsigned short width, unsigned short height,
unsigned int blockedPercent, data blockSymbol,
data noblockedSymbol):grid<data>(noblockedSymbol, width, height)
{
blockedSymbol = blockSymbol;
unblockedSymbol = noblockedSymbol;
actualheight = height;
actualwidth = width;
entranceX = 1;
entranceY = 0;
exitX = width - 2;
exitY = height - 1;
changerow(blockedSymbol, 0);
changerow(blockedSymbol, width - 1);
changecolumn(blockedSymbol, 0);
changecolumn(blockedSymbol, height - 1);
srand(time(0));
for (int r = 1; r <= height - 2; r++)
{
for (int c = 1; c <= width - 2; c++)
{
if (rand() % 100 < (blockedPercent))
changecell(blockedSymbol, c, r);
}
}
changecell(unblockedSymbol, entranceX, entranceY);
changecell(unblockedSymbol, exitX, exitY);
//Allows the mouse to do something,
//leaves 3 cells, around entrance and exit, open;
changecell(unblockedSymbol, entranceX, entranceY + 2);
changecell(unblockedSymbol, entranceX, entranceY + 1);
changecell(unblockedSymbol, entranceX + 1, entranceY + 1);
changecell(unblockedSymbol, exitX, exitY - 2);
changecell(unblockedSymbol, exitX, exitY - 1);
changecell(unblockedSymbol, exitX - 1, exitY - 1);
cout << "height: " << height
<< " width: " << width
<< " blocked%: " << blockedPercent
<< " blocked: " << blockedSymbol
<< " unblocked: " << unblockedSymbol
<< endl;
}
template <class data>
maze<data>::maze(maze & m)//:grid<data>(grid<data> & m)
{
entranceX = m.entranceX;
exitX = m.exitX;
entranceY = m.entranceY;
exitY = m.exitY;
blockedSymbol = m.blockedSymbol;
unblockedSymbol = m.unblockedSymbol;
actualheight = m.actualheight;
actualwidth = m.actualwidth;
}
template <class data>
bool maze<data>::setEntrance(unsigned short e)
{
if (e < actualheight && e > 1)
{
entranceX = 0;
entranceY = e;
return true;
}
else
return false;
}
template <class data>
bool maze<data>::setExit(unsigned short e)
{
if (e < actualheight && e > 1)
{
exitX = actualwidth;
exitY = e;
return true;
}
else
return false;
}
/*
template <class data>
data maze<data>::getLattice(unsigned short x, unsigned short y)//:grid<data>(lattice[x][y])
{
return lattice[x][y];
}
*/
template <class data>
bool maze<data>::IsThisCellBlocked(unsigned int x, unsigned int y)
{
if (getLattice(x, y) == unblockedSymbol)
//if (lattice[x][y] == unblockedSymbol)
return false;
else
return true;
}
template <class data>
bool maze<data>::IsThisCellTheEntrance(unsigned int x, unsigned int y)
{
if (x == 0)
{
if (y == entranceY)
return true;
else
return false;
}
else
return false;
}
template <class data>
bool maze<data>::IsThisCellTheExit(unsigned int x, unsigned int y)
{
if (x == actualwidth)
{
if (y == exitY)
return true;
else
return false;
}
else
return false;
}
template <class data>
void maze<data>::getEntrance(unsigned short &x, unsigned short &y)
{
x = entranceX;
y = entranceY;
}
#endif
Above is maze.h
======================================================
PHP Code:
#ifndef GRID_H
#define GRID_H
//#include <windows.h>
#include "screen.h"
#include <iostream>
using std:: ostream;
using std::flush;
using std::endl;
template <class cell>
class grid
{
protected:
cell * * lattice;
unsigned short maxrows;
unsigned short maxcols;
public:
grid();
grid(cell initialvalue, unsigned short numberofrows,
unsigned short numberofcols);
grid(grid & g);
bool changerow(cell newvalue, unsigned int rownumber);
bool changecolumn(cell newvalue, unsigned int columnnumber);
bool changecell(cell newvalue, unsigned int rownumber, unsigned int colnumber);
void changegrid(cell newvalue);
cell getLattice(int x, int y);
friend ostream & operator << (ostream & o, const grid<cell> g);
~grid();
};
template <class cell>
grid<cell>::grid()
{
maxrows = 1;
maxcols = 1;
lattice = new cell * [1];
lattice[0] = new cell [1];
}
template <class cell>
grid<cell>::grid(cell initialvalue, unsigned short numberofrows,
unsigned short numberofcols)
{
maxrows = numberofrows;
maxcols = numberofcols;
lattice = new cell * [numberofrows];
for (int r = 0; r < numberofrows; r++)
{
lattice[r] = new cell [numberofcols];
for (int c = 0; c < numberofcols; c++)
lattice[r][c] = initialvalue;
}
}
template <class cell>
grid<cell>::grid(grid & g)
{
maxrows = g.maxrows;
maxcols = g.maxcols;
lattice = new cell * [maxrows];
for (int r = 0; r < maxrows; r++)
{
lattice[r] = new cell [maxcols];
for (int c = 0; c < maxcols; c++)
lattice[r][c] = g.lattice[r][c];
}
}
template <class cell>
bool grid<cell>::changerow(cell newvalue, unsigned int rownumber)
{
if (rownumber < maxrows)
{
for (int c = 0; c < maxcols; c++)
lattice[rownumber][c] = newvalue;
return true;
}
else
return false;
}
template <class cell>
bool grid<cell>::changecolumn(cell newvalue, unsigned int columnnumber)
{
if (columnnumber < maxcols)
{
for (int r = 0; r < maxrows; r++)
lattice[r][columnnumber] = newvalue;
return true;
}
else
return false;
}
template <class cell>
bool grid<cell>::changecell(cell newvalue, unsigned int rownumber, unsigned int colnumber)
{
if ( (rownumber < maxrows) && (colnumber < maxcols) )
{
lattice[rownumber][colnumber] = newvalue;
return true;
}
else
return false;
}
template <class cell>
void grid<cell>::changegrid(cell newvalue)
{
for (int r = 0; r < maxrows; r++)
for (int c = 0; c < maxcols; c++)
lattice[r][c] = newvalue;
}
template <class cell>
cell grid<cell>::getLattice(int x, int y)
{
return lattice[x][y];
}
template <class cell>
ostream & operator << (ostream & o, grid<cell> g)
{
//Clear screen and set
//CURSOR AT TOP LEFT!!!
ClearScreen();
// PRINT MAZE
for (int r = 0; r < g.maxrows; r++)
{
for (int c = 0; c < g.maxcols; c++)
cout << g.lattice[r][c];
cout << endl;
}
return o;
}
template <class cell>
grid<cell>::~grid()
{
for (int r =0; r < maxrows; r++)
delete [] lattice[r];
delete [] lattice;
}
#endif
//END grid.h
Above is grid.h
====================================================
PHP Code:
#include <iostream>
using namespace std;
#include "mouse.h"
#include "maze.h"
void main()
{
auto unsigned int mazeWidth;
auto unsigned int mazeHeight;
auto unsigned int mazePercent;
auto bool bRunning = true; //Main game loop
// auto char nothin;
cout << "Enter width: ";
cin >> mazeWidth;
cout << "Enter height: ";
cin >> mazeHeight;
cout << "Enter Maze coverage (%): ";
cin >> mazePercent;
if (mazeHeight > 24)
mazeHeight = 24;
if (mazeWidth >= 79)
mazeWidth = 79;
maze<char> disneyland(mazeHeight, mazeWidth, mazePercent, 'X', ' ');
mouse<char> mickey(disneyland, '#');
//mouse mickey;
cout << disneyland;
cout << mickey;
while (bRunning)
{
mickey.takeOneStep(disneyland);
cout << mickey;
bRunning = false;
}
}
Above is main.
Thanks once again Z!
NOMAD
-
Apr 18th, 2002, 08:04 PM
#4
It works fine. Your problem is that you dont check your matrix indices. It crashes when y == -1.
I am assuming that this is a school assignment. If you have MSVC, I suggest you learn to use the debugger, if you dont already know =). It will help you solve these kinds of problems without hardly any thought (when your program crashed, I could immediately see that y == -1, because all of the values were presented to me =). Get to know the thing, and you will be able to solve your problems so much easier =).
Z.
-
Apr 19th, 2002, 12:16 PM
#5
Z, thanks again. I'm sorry I didn't check the debugger more completly. I feel bad for running to the forum when ever I hit the smallest of walls. I guess I'm feeling stressed because this is due monday. Anyway, thanks for your help and I'll try to keep the questions to a minimum!
NOMAD
(Z Rocks!)
-
Apr 19th, 2002, 04:08 PM
#6
Dont worry about posting here, when you really are stumped. Just try your best to find the problem first. The real fun in programming is figuring out why stuff doesnt work =).
Z.
-
Apr 19th, 2002, 05:15 PM
#7
Monday Morning Lunatic
Originally posted by Zaei
Dont worry about posting here, when you really are stumped. Just try your best to find the problem first. The real fun in programming is figuring out why stuff doesnt work =).
Z.
.....and getting paid for it
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 19th, 2002, 05:37 PM
#8
True, True.
I had to explain the evils of void main() this morning... I dont think they took me seriously =).
Z.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|