Debugging a stack class

This is a discussion on Debugging a stack class within the C++ Programming forums, part of the General Programming Boards category; I refer to this website a lot, and there seems to be a lot of very helpful support from its ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Debugging a stack class

    I refer to this website a lot, and there seems to be a lot of very helpful support from its members, so I decided to join the forum. I am currently making my own solution to an n queens problem and ran into a snag with some of my class member functions.
    First of all, I know that this is not the best way to do this, but it's the first time I've taken a stab at an n queens problem.

    Here are my function definitions:
    Code:
    Queen::Queen()
    {
         queenLoc = new s_type[8];
         //Creates array for queens with generic value (until input received)
         numQueens = 8;
         place = 0;
         success() == 0;
    }
    
    Queen::~Queen()
    {
         delete []queenLoc;
    }    
    
    Queen::Queen(int boardsize)
    {
         queenLoc = new s_type[boardsize];
         numQueens = boardsize; //Takes user input, makes it size of board
         place = 0;
         placement();//Begins placing queens...
    }  
    
    void Queen::push(s_type s)
    {
         if(success() == 0) //Will only push to stack if no solution yet
         {
                    queenLoc[place] = s; //Pushes location of queen to stack
                    place++; //Increments number of places used by 1
                    return;
         }
         else if(success() == 1)
         cout << "There is a solution!" << endl;
         else
         cout << "An error has occurred..." << endl;
         //This final else statement is included for error checking
    }
    
    Queen::s_type Queen::pop()
    {
         s_type topqueenLoc;
         
         if(counter() == 0)//Prevents popping from empty stack
         {
                      return(topqueenLoc);
         }
         topqueenLoc = queenLoc[--place];
         return(topqueenLoc);
    }
    
    void Queen::placement()
    {
         
         while(success() == 0 && !counter() == (numQueens))
         {
                      if(conflict() == 1)
                      {
                           if(queenLoc[place] >= numQueens)
                           {
                                pop();
                           }
                           while(queenLoc[place] < numQueens)
                           {
                                queenLoc[place]++;
                           }
                                  
                      }
                      else if(conflict() == 0 && counter() == (numQueens))
                      {
                           success() == 1; //Calls suceess to return 1
                           printQueens(); //Calls function to print queens
                      }
                      else
                      {
                           push(1);
                      }
    }
    
    void Queen::printQueens()
    {
         int i;
         for(i = 0; i <= numQueens; i++)
         cout << "Queen row " << i << ": column " << queenLoc[i] << endl;
    }
    
    int Queen::counter()
    {
        return(place);
    }
    
    bool Queen::conflict()
    {
         int i,j;
         for(i = 0; i <= numQueens; i++)
         {
              for(j = 1; j <= place; j++)
              {
                    if(queenLoc[j] == i || (abs(queenLoc[j]-i) == (abs(j-place)))
                    return 0;
              }
         }
         return 1;
    }
    
    bool Queen::success()
    {
         if(counter() == (numQueens))
         return 1; //Returns 1 if all queens have been placed without conflict
         else if(counter() != (numQueens))
         return 0; //Returns 0 if a solution has not been found
         else
         cout << "A strange error has occurred..." << endl;
         //I incorporated this else statement as a means of error checking,
         //if this is ever triggered then I know something is wrong with my code...
    }
    The problem lies in the printQueens(), counter(), conflict(), and success() functions. My compiler (Dev C++) is kicking the following errors at me for each of them:

    "expected primary-expression before "void (or int, or bool, respectively)" "
    and
    "expected `;' before "void (or int, or bool, respectively""

    I know it's a very simple mistake, and I can't, for the life of me, figure this out! Any help is greatly appreciated, and I'll keep churning away at it...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    I suggest that you just use std::stack from <stack> to implement your Queen class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This line:
    Code:
    while(success() == 0 && !counter() == (numQueens))
    doesn't have a close curly brace corresponding.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    This line:
    Code:
    while(success() == 0 && !counter() == (numQueens))
    doesn't have a close curly brace corresponding.
    And... that did it, I knew it was a simple mistake

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by laserlight View Post
    I suggest that you just use std::stack from <stack> to implement your Queen class.
    I plan on converting this to a template stack class eventually, I'm only working with ints in this case so I just did what was easiest at the time. Thanks for the suggesstion though, I will definitely do it after I get all the kinks out of my code.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    I plan on converting this to a template stack class eventually, I'm only working with ints in this case so I just did what was easiest at the time
    No, I did not recommend that you turn your class into a class template. Rather, I suggested that you use an existing specialised container (as provided by the std::stack class template) to help you implement your class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    No, I did not recommend that you turn your class into a class template. Rather, I suggested that you use an existing specialised container (as provided by the std::stack class template) to help you implement your class.
    In other words... why reinvent the wheel? Especially when your own wheel will almost certainly be inferior to the existing one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 09:52 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 06:29 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21