stack container class w/o STL

I got an assignment for my c++ class to create a boundedstack without using the stl container class. this one was for the array implementation.
I wrote the code, and it built/compiled, but when I ran it, it just returned
RUN FAILED (exit value 1, total time: 519ms)
I am using netbeans, and I created a project when I started. also I tried removing the .cpp implementation from the project to no avail.
I based the code on the one in my book, and my teacher is really slow at responding to emails, so anybody who can point out any mistakes would be really helpful.
Thanks ahead of time


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//client:
#include <cstdlib>
#include "VectorStak.h"
#include <iostream>
#include <string>

/*
 * 
 */
int main(int argc, char** argv) {
    std::string log = "weblog_unique.url.txt";
    VectorStak<std::string> BoundedStackA(log);
    BoundedStackA.getlog();
    BoundedStackA.PrintStak();
    return 0;
}

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
//Header:
#ifndef VECTORSTAK_H
#define	VECTORSTAK_H

#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cassert>
template<class Item>
class VectorStak {
public:
    Item size();
    bool isEmpty();
    void push(Item e);
    void pop();
    Item peek(int i); // not used, but returns next item in stack
    VectorStak();
    VectorStak(std::string log);
    void getlog();
    void PrintStak();
    private:
        std::vector<Item> elems;
        int numUsed;
        Item Line;
        std::string Weblog;
        
};
#include "VectorStak.cpp"
#endif	/* VECTORSTAK_H */ 

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
//Implementation:
template <class Item>
VectorStak<Item>::VectorStak() {
    numUsed = 0;
}
template <class Item>
VectorStak<Item>::VectorStak(std::string log) {
    numUsed = 0;
    Weblog = log;
}

template <class Item>
bool VectorStak<Item>::isEmpty(){
    return elems.empty();
}
template <class Item>
void VectorStak<Item>::push(Item e){
    if(numUsed != 50){
        elems[numUsed] = e;
        numUsed++;
    }
    if(numUsed == 50){
         VectorStak::pop();
       elems[numUsed] = e;
    }
}
template <class Item>
void VectorStak<Item>::pop(){
    for(int i=0; i <= numUsed ; i++){
            elems[i] = elems[i+1];
        }
    numUsed--;
}
template <class Item>
Item VectorStak<Item>::peek(int i){
    return elems[i];
}
template <class Item>
Item VectorStak<Item>::size(){
    return elems.size();
}
template <class Item>
void VectorStak<Item>::getlog() { //included from assignment 2
    //This function will and add every line
    //  of a specified file to an array
    std::ifstream getStuff;
    Line = "null";
    getStuff.open("weblog_unique.url.txt"); //Weblog.c_str());
    assert(getStuff);

    while (getline(getStuff, Line)) {
        push(Line);
        // std::cout << log.GetArrayLine(i) << std::endl;
    }
    getStuff.close();
    getStuff.clear();
}
template <class Item>
void VectorStak<Item>::PrintStak(){
     for(int i=0; i <=50 ; i++){
         std::cout << "Contents of Stack: " << std::endl << elems[i] << std::endl;
        }
}
closed account (D80DSL3A)
std::vector is a STL container class. Are you sure you are allowed to use it in your program?
Since your stack has a constant 50 elements it seems like a regular array of 50 Items would work fine instead.
ie. In your VectorStak class declare as a member Item elems[50]; instead of
std::vector<Item> elems;

If you can use the vector then I believe the problem is that your vector is empty. No elements have been allocated. Use the std::vector::reserve() function to reserve space for 50 elements. The constructor would be a good place for this.
ok yeah. I wasn't supposed to use vector, and declaring an array, as well as changing my for loop to be for(int i=0; i < numUsed-1 ; i++) fixes it.
thankyou.
Topic archived. No new replies allowed.