Write your question here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
template<typename T>
class node
{
private:
node<T>* next;
T data;
public:
node(T newdata, node<T>* thenext)
{
data= newdata;
next=thenext;
}
void getdata()
{
return data;
}
void getnext()
{
return next;
}
};
#include "node.h"
template <typename T>
class stack
{
private:
int count;
node<T> *topnode;
public:
gettop()
{
if(!isempty()){
int result;
result= topnode->getdata();
return result;
}
else {
cout<<"stack underflow";
exit(0);
}
}
bool isempty()
{
return (count==0);
}
void pop(){
node<T>*oldnode= topnode;
T result= topnode;
topnode=topnode->getnext;
delete oldnode;
count--;
}
void push(T data){
node<T>* newnode = new node<T>(data, topnode);
topnode = newnode;
count++;
}
int getsize(){
return count;
}
};
#include <cstdlib>
#include <iostream>
using namespace std;
#include "stack.h"
/*
*
*/
int main()
{
stack<int> MyIntStack; // MyIntStack is the object
for (int i=0; i<5; ++i) MyIntStack.push(i);
cout << "Popping out elements";
while (!MyIntStack.isempty())
{
cout << ' ' << MyIntStack.gettop();
MyIntStack.pop();
}
cout <<endl<<MyIntStack.getsize();
return 0;
}
|
keep getting errors in my stack class and main cpp just want to see this program run here are my error codes. been at this for hours and my brain is fried need help :(. thank you so much in advanced if you can help me get this to run
In file included from main.cpp:18:0:
stack.h:28:14: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
int topnode=0;
^
stack.h: In member function 'int stack<T>::gettop()':
stack.h:38:20: error: base operand of '->' is not a pointer
result= topnode->getdata;
^
stack.h: In member function 'int stack<T>::pop()':
stack.h:59:20: error: base operand of '->' is not a pointer
topnode=topnode->getnext;
^
stack.h: In instantiation of 'int stack<T>::push(T) [with T = int]':
main.cpp:28:44: required from here
stack.h:69:52: error: invalid conversion from 'int' to 'node<int>*' [-fpermissive]
node<T>* newnode = new node<T>(data, topnode);
^
In file included from stack.h:17:0,
from main.cpp:18:
node.h:28:3: note: initializing argument 2 of 'node<T>::node(T, node<T>*) [with T = int]'
node(T newdata, node<T>* thenext)
^
In file included from main.cpp:18:0:
stack.h:70:35: error: invalid conversion from 'node<int>*' to 'int' [-fpermissive]
topnode = newnode;
^
stack.h: In instantiation of 'int stack<T>::pop() [with T = int]':
main.cpp:34:21: required from here
stack.h:55:22: error: invalid conversion from 'int' to 'node<int>*' [-fpermissive]
node<T>*oldnode= topnode;
^
nbproject/Makefile-Debug.mk:66: recipe for target 'build/Debug/Cygwin_1-Windows/main.o' failed
make[2]: *** [build/Debug/Cygwin_1-Windows/main.o] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/jschw_000/Documents/NetBeansProjects/CppApplication_2'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/jschw_000/Documents/NetBeansProjects/CppApplication_2'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total tim