blick bei templates nicht durch



  • hallo,

    folgender code:

    StackArray.h

    #include <iostream>
    
    // Defines a Stack with a fixed size
    // --> Stack Implementation with array
    
    template <class T>
    class StackArray
    {
            private:
                    unsigned int top;
                    unsigned int max;
                    T *p;
    
            public:
                    // Constructor allocates the array
                    StackArray(unsigned int m) : top(0), max(m){ p = new T[m]; }
    
                    bool isFull(void) { return top == max; }
    
                    bool isEmpty(void) { return top == 0; }
    
                    void Push(T e);
    
                    T Pop(void);
    };
    

    StackArray.cpp

    #include "StackArray.h"
    
    template <class T>
    void StackArray<T>::Push(T e)
    {
            if ( !isFull() )
            {
                    p[top] = e;
                    top++;
            }
    }
    
    template <class T>
    T StackArray<T>::Pop(void)
    {
            T e;
            if ( !isEmpty() )
            {
                    top--;
                    e = p[top];
            }
    
            return e;
    }
    

    und main.cpp

    #include "StackArray.h"
    
    using namespace std;
    
    int main(void)
    {
            StackArray<unsigned int> s(10);
            unsigned int x;
    
            while((cin >> x) && !s.isFull())
                    s.Push(x);
    
            while (!s.isEmpty())
                    cout << s.Pop() << endl;
    
    return 0;
    }
    

    Als Fehlermeldung erhalte ich:

    /tmp/ccRnxU8Z.o: In function `main':main.cpp:(.text+0x3f): undefined reference to `StackArray<unsigned int>::Push(unsigned int)'
    :main.cpp:(.text+0x9c): undefined reference to `StackArray<unsigned int>::Pop()'
    collect2: ld gab 1 als Ende-Status zurück
    

    Ich peils einfach nicht 😡



  • Die Implementierung von Templates muss mit in den Header.



  • Mein Gott 🙄



  • curry-king schrieb:

    Mein Gott 🙄

    Gabs schon mindestens 1000 Mal. Steht sogar in der FAQ, wenn mich nicht alles täuscht.



  • David_pb schrieb:

    curry-king schrieb:

    Mein Gott 🙄

    Gabs schon mindestens 1000 Mal. Steht sogar in der FAQ, wenn mich nicht alles täuscht.

    sry


Anmelden zum Antworten