Thread: Debugging a stack class

  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
    28,412
    I suggest that you just use std::stack from <stack> to implement your Queen class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10:52 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM