Results 1 to 8 of 8

Thread: More Problems from Nomad!

  1. #1
    NOMADMAN
    Guest

    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

  2. #2
    Zaei
    Guest
    Are you sure you are initializing the array correctly?

    Post the code.

    Z.

  3. #3
    NOMADMAN
    Guest
    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 widthunsigned short height
            
    unsigned int blockedPercentdata 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 xunsigned int y);
        
    bool IsThisCellTheEntrance(unsigned int xunsigned int y);
        
    bool IsThisCellTheExit(unsigned int xunsigned int y);
        
        
    void getEntrance(unsigned short &xunsigned 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>(unblockedSymbol1010)
    {
    }

    template <class data>
    maze<data>::maze(unsigned short widthunsigned short height,
               
    unsigned int blockedPercentdata blockSymbol
            
    data noblockedSymbol):grid<data>(noblockedSymbolwidthheight)
    {
        
    blockedSymbol blockSymbol;
        
    unblockedSymbol noblockedSymbol;

        
    actualheight height;
        
    actualwidth width;

        
    entranceX 1;
        
    entranceY 0;
        
    exitX width 2;
        
    exitY height 1;

        
    changerow(blockedSymbol0);
        
    changerow(blockedSymbolwidth 1);
        
    changecolumn(blockedSymbol0);
        
    changecolumn(blockedSymbolheight 1);

        
    srand(time(0));

        for (
    int r 1<= height 2r++)
        {
            for (
    int c 1<= width 2c++)
            {
                if (
    rand() % 100 < (blockedPercent))
                    
    changecell(blockedSymbolcr);
            }
        }

        
    changecell(unblockedSymbolentranceXentranceY);
        
    changecell(unblockedSymbolexitXexitY);

        
    //Allows the mouse to do something, 
        //leaves 3 cells, around entrance and exit, open; 
        
    changecell(unblockedSymbolentranceXentranceY 2);
        
    changecell(unblockedSymbolentranceXentranceY 1);
        
    changecell(unblockedSymbolentranceX 1entranceY 1);
        
    changecell(unblockedSymbolexitXexitY 2);
        
    changecell(unblockedSymbolexitXexitY 1);
        
    changecell(unblockedSymbolexitX 1exitY 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 (
    actualheight && 1)
        {
            
    entranceX 0;
            
    entranceY e;
            return 
    true;
        }
        else
            return 
    false;
    }

    template <class data>
    bool maze<data>::setExit(unsigned short e)
    {
        if (
    actualheight && 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 xunsigned int y)
    {
        if (
    getLattice(xy) == unblockedSymbol)
        
    //if (lattice[x][y] == unblockedSymbol)
            
    return false;
        else
        return 
    true;
    }

    template <class data>
    bool maze<data>::IsThisCellTheEntrance(unsigned int xunsigned int y)
    {
        if (
    == 0)
        {
            if (
    == entranceY)
                return 
    true;
            else
                return 
    false;
        }
        else
            return 
    false;
    }

    template <class data>
    bool maze<data>::IsThisCellTheExit(unsigned int xunsigned int y)
    {
        if (
    == actualwidth)
        {
            if (
    == exitY)
                return 
    true;
            else
                return 
    false;
        }
        else
            return 
    false;
    }

    template <class data>
    void maze<data>::getEntrance(unsigned short &xunsigned short &y)
    {
        
    entranceX;
        
    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 initialvalueunsigned short numberofrows,
                
    unsigned short numberofcols);
            
    grid(grid g);
            
    bool changerow(cell newvalueunsigned int rownumber);
            
    bool changecolumn(cell newvalueunsigned int columnnumber);
            
    bool changecell(cell newvalueunsigned int rownumberunsigned int colnumber); 
            
    void changegrid(cell newvalue);

            
    cell getLattice(int xint y);
            
            
    friend ostream operator << (ostream o, const grid<cellg);
            ~
    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 initialvalueunsigned short numberofrows,
                     
    unsigned short numberofcols)
    {
        
    maxrows numberofrows;
        
    maxcols numberofcols;
        
    lattice = new cell * [numberofrows];
        
        for (
    int r 0numberofrowsr++)
        {
            
    lattice[r] = new cell [numberofcols];
            for (
    int c 0numberofcolsc++)
                
    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 0maxrowsr++)
        {
            
    lattice[r] = new cell [maxcols];
            for (
    int c 0maxcolsc++)
                
    lattice[r][c] = g.lattice[r][c];
        }
    }

    template <class cell>
    bool grid<cell>::changerow(cell newvalueunsigned int rownumber)
    {
        if (
    rownumber maxrows)
        {
            for (
    int c 0maxcolsc++)
                
    lattice[rownumber][c] = newvalue;
            return 
    true;
        }
        else
            return 
    false;
    }

    template <class cell>
    bool grid<cell>::changecolumn(cell newvalueunsigned int columnnumber)
    {
        if (
    columnnumber maxcols)
        {
            for (
    int r 0maxrowsr++)
                
    lattice[r][columnnumber] = newvalue;
            return 
    true;
        }
        else
            return 
    false;
    }

    template <class cell>
    bool grid<cell>::changecell(cell newvalueunsigned int rownumberunsigned 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 0maxrowsr++)
            for (
    int c 0maxcolsc++)
                
    lattice[r][c] = newvalue;
    }

    template <class cell>
    cell grid<cell>::getLattice(int xint y)
    {
        return 
    lattice[x][y];
    }

    template <class cell>
    ostream operator << (ostream ogrid<cellg)
    {

        
    //Clear screen and set
        //CURSOR AT TOP LEFT!!!
        
    ClearScreen();

        
    // PRINT MAZE
        
    for (int r 0g.maxrowsr++)
        {
            for (
    int c 0g.maxcolsc++)
                
    cout << g.lattice[r][c];
            
    cout << endl;
        }

        return 
    o;
    }

    template <class cell>
    grid<cell>::~grid()
    {
        for (
    int r =0maxrowsr++)
            
    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<chardisneyland(mazeHeightmazeWidthmazePercent'X'' ');
        
    mouse<charmickey(disneyland'#');
        
    //mouse mickey;

        
    cout << disneyland;
        
    cout << mickey;

        while (
    bRunning)
        {
            
    mickey.takeOneStep(disneyland);
            
    cout << mickey;

            
    bRunning false;
        }
                

    Above is main.

    Thanks once again Z!

    NOMAD

  4. #4
    Zaei
    Guest
    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.

  5. #5
    NOMADMAN
    Guest
    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!)

  6. #6
    Zaei
    Guest
    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.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Zaei
    Guest
    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
  •  



Click Here to Expand Forum to Full Width