Why the undefined references in this simple program?

Discussion in 'C++' started by here@softcom.net, Feb 26, 2013.

  1. Guest

    I'm trying to complile and link this simple example of using a template:

    //file stack2.hpp (header)
    template<class T> class Stack {
    T* v;
    int max_size;
    int top;
    public:
    class Underflow {};
    class Overflow {};

    Stack(int s); //constructor
    ~Stack(); //destructor

    void push(T);
    T pop();
    };

    //file stack2.cpp
    #include "stack2.hpp"

    template<class T> void Stack<T>::push(T c)
    {
    if (top==max_size) throw Overflow();
    v[top] = c;
    top = top + 1;
    }
    template<class T> T Stack<T>::pop()
    {
    if (top==0) throw Underflow();
    top = top - 1;
    return v[top];
    }

    //file user2.cpp
    #include <iostream>
    #include "stack2.hpp"
    using namespace std;

    Stack<char> sc(200);

    int main(int argc, char *argv[])
    {
    sc.push('c');
    cout << sc.pop() << "\n";

    return 0;
    }

    /////////////////////////

    If I compile with: g++ stack2.cpp user2.cpp
    here's what I get:

    /tmp/cciILMx2.o: In function `main':
    user2.cpp:(.text+0x1a): undefined reference to `Stack<char>::push(char)'
    user2.cpp:(.text+0x24): undefined reference to `Stack<char>::pop()'
    /tmp/cciILMx2.o: In function `__static_initialization_and_destruction_0(int, int)':
    user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
    user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
    collect2: ld returned 1 exit status
     
    , Feb 26, 2013
    #1
    1. Advertising

  2. Öö Tiib Guest

    On Tuesday, 26 February 2013 21:45:09 UTC+2, wrote:
    > I'm trying to complile and link this simple example of using a template:
    >
    > //file stack2.hpp (header)
    >
    > template<class T> class Stack {
    > T* v;
    > int max_size;
    > int top;
    > public:
    > class Underflow {};
    > class Overflow {};
    >
    > Stack(int s); //constructor
    > ~Stack(); //destructor
    >
    > void push(T);
    > T pop();
    > };
    >
    > //file stack2.cpp


    Move its contents to stack2.h and delete the file.
    This is how templates work.
     
    Öö Tiib, Feb 26, 2013
    #2
    1. Advertising

  3. On 2/26/2013 3:04 PM, Öö Tiib wrote:
    > On Tuesday, 26 February 2013 21:45:09 UTC+2, wrote:
    >> I'm trying to complile and link this simple example of using a template:
    >>
    >> //file stack2.hpp (header)
    >>
    >> template<class T> class Stack {
    >> T* v;
    >> int max_size;
    >> int top;
    >> public:
    >> class Underflow {};
    >> class Overflow {};
    >>
    >> Stack(int s); //constructor
    >> ~Stack(); //destructor
    >>
    >> void push(T);
    >> T pop();
    >> };
    >>
    >> //file stack2.cpp

    >
    > Move its contents to stack2.h and delete the file.
    > This is how templates work.


    http://www.parashift.com/c -faq/responding-to-faqs.html

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 26, 2013
    #3
  4. Guest

    On Tuesday, February 26, 2013 12:04:17 PM UTC-8, Öö Tiib wrote:
    > On Tuesday, 26 February 2013 21:45:09 UTC+2, wrote:
    >
    > > I'm trying to complile and link this simple example of using a template:

    >
    > >

    >
    > > //file stack2.hpp (header)

    >
    > >

    >
    > > template<class T> class Stack {

    >
    > > T* v;

    >
    > > int max_size;

    >
    > > int top;

    >
    > > public:

    >
    > > class Underflow {};

    >
    > > class Overflow {};

    >
    > >

    >
    > > Stack(int s); //constructor

    >
    > > ~Stack(); //destructor

    >
    > >

    >
    > > void push(T);

    >
    > > T pop();

    >
    > > };

    >
    > >

    >
    > > //file stack2.cpp

    >
    >
    >
    > Move its contents to stack2.h and delete the file.
    >
    > This is how templates work.


    I tried that and got the following:

    $ g++ user.c
    /tmp/cc9OwIxI.o: In function `f(Stack&, int)':
    user.c:(.text+0x1f): undefined reference to `Stack::Stack(int)'
    user.c:(.text+0x39): undefined reference to `Stack::Stack(int)'
    user.c:(.text+0x4c): undefined reference to `Stack::push(char)'
    user.c:(.text+0x5d): undefined reference to `Stack::push(char)'
    user.c:(.text+0x6e): undefined reference to `Stack::push(char)'
    user.c:(.text+0x7f): undefined reference to `Stack::push(char)'
    user.c:(.text+0x89): undefined reference to `Stack::pop()'
    user.c:(.text+0xb1): undefined reference to `Stack::pop()'
    user.c:(.text+0xd9): undefined reference to `Stack::pop()'
    user.c:(.text+0x101): undefined reference to `Stack::pop()'
    user.c:(.text+0x129): undefined reference to `Stack::pop()'
    user.c:(.text+0x151): undefined reference to `Stack::~Stack()'
    user.c:(.text+0x179): undefined reference to `Stack::~Stack()'
    /tmp/cc9OwIxI.o: In function `main':
    user.c:(.text+0x1a5): undefined reference to `Stack::Stack(int)'
    user.c:(.text+0x1b6): undefined reference to `Stack::push(char)'
    user.c:(.text+0x1d3): undefined reference to `Stack::~Stack()'
    user.c:(.text+0x1ee): undefined reference to `Stack::~Stack()'
    /tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':
    user.c:(.text+0x246): undefined reference to `Stack::Stack(int)'
    user.c:(.text+0x24b): undefined reference to `Stack::~Stack()'
    collect2: ld returned 1 exit status
     
    , Feb 26, 2013
    #4
  5. Guest

    On Tuesday, February 26, 2013 12:30:11 PM UTC-8, wrote:
    > On Tuesday, February 26, 2013 12:04:17 PM UTC-8, Öö Tiib wrote:
    >
    > > On Tuesday, 26 February 2013 21:45:09 UTC+2, wrote:

    >
    > >

    >
    > > > I'm trying to complile and link this simple example of using a template:

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > //file stack2.hpp (header)

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > template<class T> class Stack {

    >
    > >

    >
    > > > T* v;

    >
    > >

    >
    > > > int max_size;

    >
    > >

    >
    > > > int top;

    >
    > >

    >
    > > > public:

    >
    > >

    >
    > > > class Underflow {};

    >
    > >

    >
    > > > class Overflow {};

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > Stack(int s); //constructor

    >
    > >

    >
    > > > ~Stack(); //destructor

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > void push(T);

    >
    > >

    >
    > > > T pop();

    >
    > >

    >
    > > > };

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > //file stack2.cpp

    >
    > >

    >
    > >

    >
    > >

    >
    > > Move its contents to stack2.h and delete the file.

    >
    > >

    >
    > > This is how templates work.

    >
    >
    >
    > I tried that and got the following:
    >
    >
    >
    > $ g++ user.c
    >
    > /tmp/cc9OwIxI.o: In function `f(Stack&, int)':
    >
    > user.c:(.text+0x1f): undefined reference to `Stack::Stack(int)'
    >
    > user.c:(.text+0x39): undefined reference to `Stack::Stack(int)'
    >
    > user.c:(.text+0x4c): undefined reference to `Stack::push(char)'
    >
    > user.c:(.text+0x5d): undefined reference to `Stack::push(char)'
    >
    > user.c:(.text+0x6e): undefined reference to `Stack::push(char)'
    >
    > user.c:(.text+0x7f): undefined reference to `Stack::push(char)'
    >
    > user.c:(.text+0x89): undefined reference to `Stack::pop()'
    >
    > user.c:(.text+0xb1): undefined reference to `Stack::pop()'
    >
    > user.c:(.text+0xd9): undefined reference to `Stack::pop()'
    >
    > user.c:(.text+0x101): undefined reference to `Stack::pop()'
    >
    > user.c:(.text+0x129): undefined reference to `Stack::pop()'
    >
    > user.c:(.text+0x151): undefined reference to `Stack::~Stack()'
    >
    > user.c:(.text+0x179): undefined reference to `Stack::~Stack()'
    >
    > /tmp/cc9OwIxI.o: In function `main':
    >
    > user.c:(.text+0x1a5): undefined reference to `Stack::Stack(int)'
    >
    > user.c:(.text+0x1b6): undefined reference to `Stack::push(char)'
    >
    > user.c:(.text+0x1d3): undefined reference to `Stack::~Stack()'
    >
    > user.c:(.text+0x1ee): undefined reference to `Stack::~Stack()'
    >
    > /tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':
    >
    > user.c:(.text+0x246): undefined reference to `Stack::Stack(int)'
    >
    > user.c:(.text+0x24b): undefined reference to `Stack::~Stack()'
    >
    > collect2: ld returned 1 exit status


    SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:

    $ g++ user2.cpp
    /tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
    user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
    user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
    collect2: ld returned 1 exit status
     
    , Feb 26, 2013
    #5
  6. Öö Tiib Guest

    On Tuesday, 26 February 2013 22:36:28 UTC+2, wrote:
    > SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:
    >
    > $ g++ user2.cpp
    > /tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
    > user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
    > user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
    > collect2: ld returned 1 exit status


    The definitions for constructor and destructor *are* *missing*.
    Define them in your stack.hpp.
     
    Öö Tiib, Feb 26, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    473
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  4. X Ryl
    Replies:
    0
    Views:
    352
    X Ryl
    Apr 21, 2006
  5. Adam
    Replies:
    10
    Views:
    659
Loading...

Share This Page