-
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
-
It means the way you're using push() doesn't have a matching definition. Would you care to post your class as well?
-
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.
-
I think it might be because this part
needs arguments in its defintion.
When you type this,
the original definition is defined as
so you get conflicting types.
-
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)?
-
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).
-
Ok thanks I have checked it, do I have to enter an argument with data type to the function or method when declaring it?
-
Yes. Take a look at declaring (and defining) functions.
-
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.
-
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?
-
We can't see line 54 of your stack implementation. Can you post your entire stack implementation file?
-
I have already posted the public,private and main function part of my program above.
-
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);
-
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
-
Since when have you ever typed the type inside a function call? (Look at all your other function calls ....)