Thread: Making stack class using vector class

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Making stack class using vector class

    I know this is just a worthless attempt to do something that has already been done. But bear with me!

    stack.h
    Code:
    #include <vector>
    
    using namespace std;
    
    class stack_2
    {
            private:
                    vector <int> vect;
                    int n;
            public:
                    stack_2();
                    ~stack_2();
                    void push(int);
                    int pop();
    };
    stack.cpp
    Code:
    #include <vector>
    #include "stack.h"
    
    using namespace std;
    
    stack_2::stack_2()
    {
            n=0;
    }
    
    stack_2::~stack_2()
    {
            vect.clear();
    }
    
    void stack_2::push(int x)
    {
            vect.push_back(x);
    }
    
    int stack_2::pop()
    {
            int t=vect.front();
    
            vect.pop_back();
    
            return t;
    }
    prog.cpp
    Code:
    #include "stack.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    //      stack_2 a;
    
    
    }
    and the makefile
    Code:
    GCC=g++
    
    all:
            $(GCC) -c *.cpp -Wall
            $(GCC) -o stack *.o -Wall
    clean:
            rm -rf *.o
            rm -rf stack
    this doesn't seem to compile and I can't figure why

    Code:
    g++ -c *.cpp -Wall
    g++-o stack *.o -Wall
    prog.o(.text+0xd): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    : undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
    prog.o(.text+0x60): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    : undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    prog.o(.text+0x9d): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    ............(and so on)
    sorry for the large post... but I think it is easy to follow
    Last edited by spank; 08-09-2007 at 02:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few comments:

    1. Header files should have include guards to prevent multiple inclusion.

    2. Avoid using directives (using namespace). In particular, you should not use them in header files as this would introduce the using directive to files that include the header file, thus defeating the purpose of namespaces.

    3. Your stack class does not do any memory management on its own (that is the point of using std::vector, isn't it?) so you do not need to implement a destructor as the one the compiler provides is good enough.

    4. I notice that you only have push and pop functionality. You should provide some way to access the top of the stack without popping. std::stack calls this function top but peek is another common name.

    5. You would also need to provide some way to determine if the stack is empty so that the class user can tell if pop() and top()/peek() can be called. The standard library containers provide the member function empty() for this purpose.

    5. I notice that your member variable n is not actually used. From what I see, you do not need it at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Thank you for the comments:

    1. You are saying that I should user #ifndef preproc comands? (like in plain C)

    2. I really don't grasp very well the namespace idea. Can you detaliate a little more about it? What does it offer and why is wrong what I'm doing?

    the rest of the function I know that I must implement. It's just a simple sketch of a stack class.
    the n is just a leftover from what I tried to do early.

    Coming from a C background it is somewhat strange how C++ manages memory. I know that the destructor of the vector class is used at exit. But it is still a wee bit strange

    (I figured out the compiling problem, it was my fault... wrong compiler and libaries )

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. You are saying that I should user #ifndef preproc comands? (like in plain C)
    Yes. Macros are often discouraged in C++, but this is a legitimate use of the preprocessor.

    2. I really don't grasp very well the namespace idea. Can you detaliate a little more about it? What does it offer and why is wrong what I'm doing?
    Well, with using namespace std, std::vector becomes available as vector. Now, suppose I want to have a stack of vectors (i.e., quantities with both magnitude and direction), and I either have the class in the global namespace, or I expose my own namespace that has my vector class. Now, there is a naming conflict: we have two classes named vector. If you did not use using namespace std in your header file that I included, however, I can continue to use std::vector for the container and vector for my own vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    how can I guard that ?

    Code:
    #ifndef stack.h
    #include "stack.h"
    #endif
    this isn't ok

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More like:
    Code:
    #ifndef STACK_H
    #define STACK_H
    
    /* insert stack header here */
    
    #endif
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This is MSVC dependent but you can use this to prevent multiple includes.

    #pragma once

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by spank View Post
    Code:
    int stack_2::pop()
    {
            int t=vect.front();
    
            vect.pop_back();
    
            return t;
    }
    You pop the back but return the front? That's neither stack nor queue, it's just... broken.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    I realized the mistake I was making there (in brewbuck post) and fixed it some time ago, I just didn't corrected the first post (thank you for noticing).

    the "#pragma once" directive doesn't work on gcc/g++ ??

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by spank
    the "#pragma once" directive doesn't work on gcc/g++ ??
    Stick with the method of using inclusion guards already described by laserlight.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. Making a Stack using Pointers
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-27-2002, 11:51 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM