Thread: Debugging help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Debugging help

    The following is incomplete code, but it is all I have written. The idea is to solve the "n queens" problem, where n is the number of queens, and the dimensions of a chessboard (e.g., 8 queens on an 8x8 board).

    The code as I have written it will allocate an 11x11 square, and then ask for the user to remove (11-n) columns and rows, n being the above.

    I keep getting an error when I run the program. It compiles, but when I enter a number to deallocate the squares, it turns up an error and I have to break the debugging.

    The code is below:

    chessboard.h
    Code:
    #pragma once#include "Row.h"
    #include "Column.h"
    
    
    class Chessboard
    {
    private:
    	Row *row[11];
    
    
    public:
    	Chessboard(void);
    	~Chessboard(void);
    	void allocateTiles();
    	void removeTiles(int num);
    };
    chessboard.cpp
    Code:
    #include "Chessboard.h"
    
    
    
    Chessboard::Chessboard(void)
    {
    }
    
    
    
    
    Chessboard::~Chessboard(void)
    {
    }
    
    
    void Chessboard::allocateTiles()
    {
    	for (int c=0; c<11; c++)
    		row[c] = new Column[11];
    
    
    	return;
    }
    
    
    void Chessboard::removeTiles(int num)
    {
    	for(num; num<11; num++)
    		delete row[num];
    
    
    	return;
    }
    row.h
    Code:
    #pragma onceclass Row
    {
    private:
    	int rowNum;
    public:
    	Row(void);
    	~Row(void);
    	void setRowNum(int num);
    	int getRowNum();
    };
    row.cpp
    Code:
    #include "Row.h"
    
    
    
    Row::Row(void)
    {
    }
    
    
    
    
    Row::~Row(void)
    {
    }
    
    
    void Row::setRowNum (int num)
    {
    	rowNum = num;
    	return;
    }
    
    
    int Row::getRowNum()
    {
    	return rowNum;
    }
    column.h
    Code:
    #include "Row.h"
    
    #pragma once
    class Column: public Row
    {
    private:
    	int colNum;
    public:
    	Column(void);
    	~Column(void);
    	void setColNum(int num);
    	int getColNum();
    };
    column.cpp
    Code:
    #include "Column.h"
    
    
    
    Column::Column(void)
    {
    }
    
    
    
    
    Column::~Column(void)
    {
    }
    
    
    void Column::setColNum (int num)
    {
    	colNum = num;
    	return;
    }
    
    
    int Column::getColNum()
    {
    	return colNum;
    }
    main.cpp
    Code:
    #include "Chessboard.h"#include <iostream>
    using namespace std;
    
    
    int main()
    {
    	Chessboard c;
    	int queens;
    
    
    	cout << "How many queens would you like to calculate for?  ";
    	cin >> queens;
    
    
    	c.removeTiles(queens);
    
    
    	system("pause");
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see a call to AllocateTiles, so how do you ever get any tiles to remove?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Added allocateTiles() to the constructor for Chessboard. Now getting a different error. Debug assertion error. What would cause that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Trey Brumley View Post
    Added allocateTiles() to the constructor for Chessboard. Now getting a different error. Debug assertion error. What would cause that?
    Well, it tells you what particular debug assertion actually did fail, so that would probably give some clues. The immediate problem may very well be you using delete instead of delete[] since you allocated an array, although ultimately I suspect it may have something to do with the fact that your class setup is completely out of whack (a Column is-a Row? Really?) for what you want to be doing.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    The following code is updated from the old code. Column.h and Column.cpp have been removed, replaced by Space.h and Space.cpp. Main.cpp is exactly the same.

    There is another break error in the code, and I think it has something to do with the fact that I'm removing a space, and not a pointer, but when I place the * before it, it fails to compile. The line being turned up in the break error message is indicated via comment.

    Chessboard.h
    Code:
    #pragma once#include "Row.h"
    
    
    class Chessboard
    {
    private:
        Row *row[11];
    
    
    public:
        Chessboard(void);
        ~Chessboard(void);
        void allocateTiles();
        void removeTiles(int num);
    };
    Chessboard.cpp
    Code:
    #include "Chessboard.h"
    
    Chessboard::Chessboard(void)
    {
        allocateTiles();
    }
    
    
    Chessboard::~Chessboard(void)
    {
    }
    
    
    void Chessboard::allocateTiles()
    {
        for (int x = 0; x<11; x++)
            row[x] = new Row;
    
    
        return;
    }
    
    
    void Chessboard::removeTiles(int num)
    {
        int x = num;
    
    
        for(x; x<11; x++)
            delete row[x];
    
    
        for (x=num; x>0; x--)
            row[x]->deleteSpaces(num);
    
    
        return;
    }
    Row.h
    Code:
    #include "Space.h"
    
    #pragma once
    class Row
    {
    private:
        int rowNum;
        Space *space[11];
    public:
        Row(void);
        ~Row(void);
        void setRowNum(int num);
        int getRowNum();
        void deleteSpaces(int x);
    };
    Row.cpp
    Code:
    #include "Row.h"
    
    Row::Row(void)
    {
    }
    
    
    Row::~Row(void)
    {
    }
    
    
    void Row::setRowNum (int num)
    {
        rowNum = num;
        return;
    }
    
    
    int Row::getRowNum()
    {
        return rowNum;
    }
    
    
    void Row::deleteSpaces(int x)
    {
        for(x; x<11; x++)
            delete space[x];  //This line here has the green arrow next to it.
    
    
        return;
    }
    Space.h
    Code:
    #pragma onceclass Space
    {
    private:
        int spaceNum;
    public:
        Space(void);
        ~Space(void);
        void setSpaceNum(int x);
        int getSpaceNum();
    };
    Space.cpp
    Code:
    #include "Space.h"
    
    Space::Space(void)
    {
    }
    
    
    
    
    Space::~Space(void)
    {
    }
    
    
    void Space::setSpaceNum(int x)
    {
        spaceNum = x;
        return;
    }
    
    
    int Space::getSpaceNum()
    {
        return spaceNum;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't have any spaces to delete. If you didn't get it from new, you can't feed it to delete.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    After moving allocateTiles() to the Row class and retooling it a bit, it compiles just fine. I also allocated rows, which automatically allocated spaces, creating an 11x11 board. The idea is to remove the unnecessary spaces. I enter 5, and it should remove rows 5-10 (0 indexed, to fill out 11 spaces), as well as spaces 5-10 for each still existing row. The same break error turn up, but I've allocated spaces and rows from new.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Trey Brumley View Post
    I also allocated rows, which automatically allocated spaces, creating an 11x11 board.
    This statement is false (unless you have greatly changed your code since the last posting). No spaces are created when you create a row.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Code:
    Row::Row(void){
    	allocateSpaces(11);
    }
    Code:
    void Row::allocateSpaces(int num)
    {
    	for (int x=0; x<num; x++)
    		space[x] = new Space;
    
    
    	return;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    for (x=num; x>0; x--)
            row[x]->deleteSpaces(num);
    row[num] no longer exists (since you just deleted it), so trying to follow that pointer can only lead to heartbreak. Presumably you should start with num-1 instead.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Thanks. That got it working. Everything else is just some good old fashioned logic now, since it's all output.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe you should consider using vectors instead of raw pointers. It will give you less headaches.
    You are also violating the Rule of Three.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with debugging please
    By Kirch21 in forum C Programming
    Replies: 2
    Last Post: 02-01-2012, 10:49 PM
  2. Need help Debugging (ADT)
    By minidragon in forum C Programming
    Replies: 16
    Last Post: 08-08-2011, 06:39 PM
  3. Need help debugging...
    By n2d33p88 in forum C++ Programming
    Replies: 8
    Last Post: 12-13-2007, 04:15 AM
  4. help with debugging
    By Mark S. in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2005, 11:50 PM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM