Thread: About stack program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    About stack program

    Hi,

    I have made a class for stack program on my Dev C++ and I have declared the objects that would be used in the program.


    ---------------------------------------------------------------------

    Code:
    int main()
               
     {
    
    
                 Stack stack; // creating a stack object/
                 
                 //Pushing 5 elements on the stack/
                 
                 for(int p=0; p<11; p++)
                 
                 {
                   if(!stack.IsFull()) //checking if stack is full or not/
                   {
    Line 54 ---> stack.push(p);     // push the element on the stack/
                   }
                   else
                     cout <<"\n Stack is full,can't inset new element";
                     
                     }
                     
                     //pop the elements at the stack
                     
                     for(int i=0; i<12; i++)
                     
                     {
                     if (!stack.Isempty()) //checks if stack is empty or not/
                     
                     stack.pop();         
                     }
                     else
                     
                     cout<<"stack is Empty ,can't pop elements";
                     
                     }
    ------------------------------------------------------------------------- ----------------------

    when I try to compile this program ,it gives me an error on the line number 54 as:

    54 E:\data\Stack Implementation.cpp no matching function for call to`Stack push(int&)'

    --------------------------------------------------------------------

    I have declared the variables in the private part of this class

    I have shown this line number on the above program.

    I wanted to know that what does this error actually means?


    Thanks
    Last edited by student111; 11-18-2013 at 01:36 PM. Reason: correcting.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It means the way you're using push() doesn't have a matching definition. Would you care to post your class as well?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    this is the remaining part of the class for stack program, I am actually using the method push() and have declared the object stack before it.

    ----------------------------------------------------------------------------------------------------------------
    Code:
      #include<iostream>
      #define maxstacksize 10
      #include <stack> 
     
      using namespace std;
      //stack implemented using array/
           
      class Stack        
         {
         public:  
        int x[10]; //array for holding stack elements/
    //member functions of the stack/
                              
      Stack();//constructor/
      void push();       
      int pop();      
      int Isempty();      
      int IsFull();
     ~Stack();
            
              
             
             
             
               private:                   
               int object;          //The data element/
               int current;        // Index of the array/
               int size;           //max size of the array/
               int X[5];          // Array of 5 elements/    
               int p;             //variable for storing the plate identity/
               
               
               };
    -----------------------------------------------------------------------------------------------------------------

    I am re writing this error here.
    54 .Stack Implementation.cpp no matching function for call to `Stack:: Push(int&)'

    Their is a double colon between "Stack" and "Push" in this error ,although I am making a simple class.
    Last edited by student111; 11-18-2013 at 01:50 PM. Reason: correcting

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think it might be because this part
    Code:
    void push();
    needs arguments in its defintion.

    When you type this,
    Code:
    stack.push(p);
    the original definition is defined as
    Code:
    void push(void);
    so you get conflicting types.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Ok, so it means that if I want to pass an argument or parameter to a method or a function, I need to define its return type (except for void)?
    Last edited by student111; 11-18-2013 at 02:04 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have declared your push function to take no arguments, but you pass one argument to it.
    You need to declare your push function to take an argument to push onto the stack.
    Also, your indentation could use some improvement. I would also get rid of Dev-C++ for a modern IDE and compiler (see: SourceForge.net: Integrated Development Environment - cpwiki).
    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.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Ok thanks I have checked it, do I have to enter an argument with data type to the function or method when declaring it?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. Take a look at declaring (and defining) functions.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    do you perhaps come from a background in C? in C, a declaration with no parameters allows the definition to have any parameters. this is not the case in C++. if your definition takes one parameter, your declaration must also take one parameter.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    After checking and correcting it, when I compile this program, it gives me an error message of
    syntax error as:

    54 E:\ data\Stack Implementation.cpp syntax error before `)' token

    I have changed the push method that I declared in the public part of the class stack, i have changed the return type for the push method as "int" and have passed the argument as "int p" to this method.

    Can you pls tell me why am I having this error message?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We can't see line 54 of your stack implementation. Can you post your entire stack implementation file?
    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.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    I have already posted the public,private and main function part of my program above.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    This is what I did,

    I simply written "int" before push method and passed an argument "int p" to it ,when declaring it in public part of the class stack as:

    int push(int p); //for pushing element on the stack/

    int p; //variable for holding a number/

    In the main function , I have written:

    stack.push(int p);


  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    This is the program of the stack class, I have highlighted the changes with bold that I did.

    ---------------------------------------------------------------------------------------------------------------------------------------
    Code:
     #include<iostream>
      #define maxstacksize 10
      #include <stack> 
     
      using namespace std;
      //stack implemented using array/
           
      class Stack        
         {
         public:  
    
    //member functions of the stack/
                              
      Stack();//constructor/
      int push(int p);       
      int pop();      
      int Isempty();      
      int IsFull();
      int p;  
      int num;
     ~Stack();//destructor/
            
              
             
             
             
               private:                   
               int object;          //The data element/
               int current;        // Index of the array/
               int size;           //max size of the array/
               int x[10]; 
               
               };           
               
               int main()
                {
                 Stack stack; // creating a stack object/
                 
                 //Pushing 5 elements on the stack/
                 
                 for(int p=0; p<11; p++)
                 
                 {
                   if(!stack.IsFull()) //checking if stack is full or not/
                   {
     54 line  ------> stack.push(int p); // push the element on the top/
                   }
                   else
                    
                    {
                     cout <<"\n Stack is full,can't inset new element";
                     
                     }
    
                     //pop the elements at the stack
                     
                     for(int p=0; p<11; p++)
                     
                     {
                     if (!stack.Isempty()) //checks if stack is empty or not/
                     {
                     stack.pop();         
                     }
                     else
                     
                     cout<<"stack is Empty ,can't pop elements";
                     
                     } 
                     
                               
                
    system("pause");
                     
    }
    ---------------------------------------------------------------------------------------------------------------------------------------
    Now when I try to compile this program, I get this error:

    54 E:\ data\Stack Implementation.cpp syntax error before `)' token
    Last edited by student111; 11-19-2013 at 04:38 AM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since when have you ever typed the type inside a function call? (Look at all your other function calls ....)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack program
    By student111 in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2013, 01:39 AM
  2. Explain me how the stack looks like for any C program
    By ckarthickit in forum C Programming
    Replies: 2
    Last Post: 06-10-2010, 03:42 PM
  3. Stack program
    By trishtren in forum C Programming
    Replies: 1
    Last Post: 10-01-2009, 01:47 PM
  4. Stack Program Error
    By DarkDot in forum C++ Programming
    Replies: 12
    Last Post: 03-30-2007, 04:32 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM