cant get stack program to run

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

stack.h:28:14: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
int topnode=0;

There is no such line in your code...

stack.h: In member function 'int stack<T>::gettop()':

You have (wrongfully) declared `gettop()' without specifying a return type...


Those messages don't correspond to your code, further analysis is pointless.
im so sorry gave you the old error codes wrong error codes here are the ones

cd 'C:\Users\jschw_000\Documents\NetBeansProjects\CppApplication_2'
C:\Users\jschw_000\Desktop\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/jschw_000/Documents/NetBeansProjects/CppApplication_2'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_1-Windows/cppapplication_2.exe
make[2]: Entering directory '/cygdrive/c/Users/jschw_000/Documents/NetBeansProjects/CppApplication_2'
mkdir -p build/Debug/Cygwin_1-Windows
rm -f "build/Debug/Cygwin_1-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/Cygwin_1-Windows/main.o.d" -o build/Debug/Cygwin_1-Windows/main.o main.cpp
In file included from main.cpp:18:0:
stack.h: In instantiation of 'int stack<T>::gettop() [with T = int]':
main.cpp:33:39: required from here
stack.h:38:11: error: void value not ignored as it ought to be
result= topnode->getdata();
^
stack.h: In instantiation of 'void stack<T>::pop() [with T = int]':
main.cpp:34:21: required from here
stack.h:58:15: error: invalid conversion from 'node<int>*' to 'int' [-fpermissive]
T result= topnode;
^
stack.h:60:12: error: cannot convert 'node<T>::getnext<int>' from type 'void (node<int>::)()' to type 'node<int>*'
topnode=topnode->getnext;
^
In file included from stack.h:17:0,
from main.cpp:18:
node.h: In instantiation of 'void node<T>::getdata() [with T = int]':
stack.h:38:11: required from 'int stack<T>::gettop() [with T = int]'
main.cpp:33:39: required from here
node.h:37:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
return data;
^
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 time: 4s)
error: void value not ignored as it ought to be
result= topnode->getdata();
1
2
3
/*void*/ T getdata() {
	return data;
}


error: invalid conversion from 'node<int>*' to 'int' [-fpermissive]
T result= topnode;

`result' is `T'
`topnode' is `node<T>*'
¿what are you trying to do?

Also don't know what you want to do with `result', as you don't use it in that function.


error: cannot convert 'node<T>::getnext<int>' from type 'void (node<int>::)()' to type 'node<int>*'
topnode=topnode->getnext;

you forgot the parenthesis to call the function
topnode=topnode->getnext();

In instantiation of 'void node<T>::getdata() [with T = int]':
node.h:37:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
return data;
¿do you know what void mean?
¿do you understand what is the return type of a function?
T getdata()
Topic archived. No new replies allowed.