Help with errors please ASAP

These are the codes. my_stack.cpp was not given, the rest was given to me. Could anyone please help me get rid of the errors. Thank You.
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
126
127
128
129
130
131
//my_stack.cpp
template <class T>

my_stack::my_stack()

{

container = new vector<T>();

length = 0;

}

template <class T>

bool my_stack::empty()const

{

return container.size() == 0;

}

template <class T>

unsigned int my_stack::size()const

{

return container.size();

}

template <class T>

void my_stack::push(const T x)

{

try

{

if(length == container.size()-1)

throw new StackFullException();

else

{

container.push_back();

length++;

}

}

catch(StackFullException se)

{

cout<<"Stack is full"<<endl;

}

}

template <class T>

T my_stack::pop()

{

try{

if(empty())

throw new StackEmptyException();

else

{

return container.pop_back();

length--;

}

}

catch(StackEmptyException se)

{

cout<<"Stack is empty"<<endl;

}

}

template <class T>

T my_stack::top()

{

try{

if(empty())

throw new StackEmptyException();

else

return container[length];

}

catch(StackEmptyException se)

{

cout<<"Stack is empty"<<endl;

}

}
 

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
//my_stack.h
#ifndef MY_STACK_H
#define MY_STACK_H

// my_stack.h  -- a stack implemented on top of an STL vector

#include <vector>
#include <iostream>
#include <string>

using namespace std;


class StackException {
  public:
    StackException();
    string message; 
};

class StackEmptyException : public StackException { 
  public:
    StackEmptyException(); 
};

class StackFullException : public StackException { 
  public:
    StackFullException();
};

template <class T>
class my_stack
{
public:
   my_stack();
   bool empty() const;
   unsigned int size() const;
   void push(const T x) throw(StackFullException); 
   T pop() throw (StackEmptyException);
   T top() throw (StackEmptyException);

private:
    vector<T> container;
  int length;
};


// Notice we include the .cpp here, this is because
// template classes are not to be compiled into .o 
// files like regular source files, because the compiler
// won't know how to duplicate the code if they are.
#include "my_stack.cpp"

#endif 


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
//test_stack.cpp
#include <iostream>
#include <cassert>
#include "my_stack.h"

using namespace std;

int main(char *args[])
{
  #define STACK my_stack

  STACK<int> s1;
  bool caught = false; 
  
  try { 
    s1.top(); 
  } catch (StackEmptyException e){ 
    cout << "top() exception: '" << e.message << "'" << endl;
    caught = true; 
  }
  
  assert(caught);
  
  caught = false; 
  
  try { 
    s1.pop(); 
  } catch (StackEmptyException e){ 
    cout << "pop() exception: '" << e.message << "'" << endl;
    caught = true; 
  }
  
  assert(caught);
  
  assert(s1.size() == 0);
  assert(s1.empty());

  s1.push(16);
  assert(s1.size() == 1);
  assert(s1.top() == 16);

  s1.pop();
  assert(s1.size() == 0);

  s1.push(11);
  assert(s1.size() == 1);
  assert(s1.top() == 11);

  s1.push(22);
  assert(s1.size() == 2);
  assert(s1.top() == 22);

  s1.push(33);
  s1.push(34);
  s1.push(35);
  s1.push(36);
  s1.push(37);
  s1.push(42);
  assert(s1.size() == 8);
  assert(s1.top() == 42);

  s1.pop();
  assert(s1.size() == 7);
  assert(s1.top() == 37);

  cout << "All tests passed. Press any key to continue." << endl;
  cin.get();
}

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
//errors :(
In file included from my_stack.h:50:0,
                 from test_stack.cpp:3:
my_stack.cpp:9:1: error: invalid use of template-name ‘my_stack’ without an argument list
my_stack.cpp:21:6: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp:21:23: error: non-member function ‘bool empty()’ cannot have cv-qualifier
my_stack.cpp: In function ‘bool empty()’:
my_stack.cpp:25:8: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:31:14: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp:31:30: error: non-member function ‘unsigned int size()’ cannot have cv-qualifier
my_stack.cpp: In function ‘unsigned int size()’:
my_stack.cpp:35:8: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:41:6: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘void push(T)’:
my_stack.cpp:49:4: error: ‘length’ was not declared in this scope
my_stack.cpp:49:14: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:77:3: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘T pop()’:
my_stack.cpp:83:10: error: no matching function for call to ‘empty()’
my_stack.cpp:83:10: note: candidate is:
my_stack.cpp:21:6: note: template<class T> bool empty()
my_stack.cpp:91:8: error: ‘container’ was not declared in this scope
my_stack.cpp:93:1: error: ‘length’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:111:3: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘T top()’:
my_stack.cpp:117:10: error: no matching function for call to ‘empty()’
my_stack.cpp:117:10: note: candidate is:
my_stack.cpp:21:6: note: template<class T> bool empty()
my_stack.cpp:123:8: error: ‘container’ was not declared in this scope
my_stack.cpp:123:18: error: ‘length’ was not declared in this scope
test_stack.cpp: At global scope:
test_stack.cpp:7:5: warning: first argument of ‘int main(char**)’ should be ‘int’ [-Wmain]
test_stack.cpp:7:5: warning: ‘int main(char**)’ takes only zero or two arguments [-Wmain]
Last edited on
Topic archived. No new replies allowed.