Making stack class using vector class

This is a discussion on Making stack class using vector class within the C++ Programming forums, part of the General Programming Boards category; I know this is just a worthless attempt to do something that has already been done. But bear with me! ...

  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 03:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,272
    More like:
    Code:
    #ifndef STACK_H
    #define STACK_H
    
    /* insert stack header here */
    
    #endif
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    #pragma once

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,806
    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, 03:39 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  3. Making a Stack using Pointers
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-27-2002, 12:51 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21