1. #1
    Curahee Q's Avatar
    Registered
    07/12/07
    Location
    Hoogstraten
    Posts
    854
    iTrader
    0
    Mentioned
    0 Post(s)

    C++ Netbeans undefined reference

    Ik ben bezig in Netbeans een ArrayList te maken (met hetzelfde gedrag als deze in Java).

    Eerst had ik dit al gemaakt enkel voor doubles, en dit werkte goed. Nu probeer ik met een template te werken en geeft hij undefined reference.

    main.cpp (Source Files)
    Code:
    #include <iostream>
    #include "ArrayList.h"
    using namespace std;
    
    int main(int argc, char** argv) {
        ArrayList<double> l;
    
        return 0;
    }
    ArrayList.h (Header Files)
    Code:
    #ifndef _ARRAYLIST_H
    #define	_ARRAYLIST_H
    
    template <class T>
    class ArrayList {
        public:
            ArrayList();
            ArrayList(int capacity);
    
            int capacity() const;
            int size() const;
    
            void add(T x);
            T get(int i) const;
            void set(int i, T x);
            void clear();
    
            bool is_empty() const;
            void schrijf(char delimiter) const;
    
        private:
            int m_capacity;
            int m_size;
            T* data;
    };
    
    #endif
    ArrayList.cpp (Header Files)
    Code:
    #include <iostream>
    #include "ArrayList.h"
    
    template <class T>
    ArrayList<T>::ArrayList() {
        m_capacity = 10;
        m_size = 0;
    
        data = new T[m_capacity];
    }
    
    template <class T>
    ArrayList<T>::ArrayList(int capacity) {
        if(capacity <= 0) {
            throw "Capaciteit moet groter zijn dan 0!";
        }
    
        m_capacity = capacity;
        m_size = 0;
    
        data = new T[m_capacity];
    }
    
    template <class T>
    int ArrayList<T>::capacity() const {
        return m_capacity;
    }
    
    template <class T>
    int ArrayList<T>::size() const {
        return m_size;
    }
    
    template <class T>
    void ArrayList<T>::add(T x) {
        if(m_capacity == m_size) {
            m_capacity *= 2;
    
            T* arr = new T[m_capacity];
    
            for(int i=0; i<m_size; i++) {
                arr[i] = data[i];
            }
    
            delete[] data;
            
            data = arr;
        }
    
        data[m_size] = x;
    
        ++m_size;
    }
    
    template <class T>
    T ArrayList<T>::get(int i) const {
        if(i < 0 || i >= m_size) {
            throw "Array index out of bounds!";
        }
    
        return data[i];
    }
    
    template <class T>
    void ArrayList<T>::set(int i, T x) {
        if(i < 0 || i >= m_size) {
            throw "Array index out of bounds!";
        }
    
        data[i] = x;
    }
    
    template <class T>
    void ArrayList<T>::clear() {
    
    }
    
    template <class T>
    bool ArrayList<T>::is_empty() const {
        return m_size==0;
    }
    
    template <class T>
    void ArrayList<T>::schrijf(char delimiter=' ') const {
        for(int i=0; i<m_size-1; i++) {
            std::cout << data[i] << delimiter;
        }
    
        std::cout << data[m_size-1];
    }
    Error
    Code:
    /usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
    make[1]: Map '/home/sam/NetBeansProjects/CppApplication_1' wordt binnengegaan
    /usr/bin/make  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cppapplication_1
    make[2]: Map '/home/sam/NetBeansProjects/CppApplication_1' wordt binnengegaan
    mkdir -p build/Debug/GNU-Linux-x86
    rm -f build/Debug/GNU-Linux-x86/ArrayList.o.d
    g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/ArrayList.o.d -o build/Debug/GNU-Linux-x86/ArrayList.o ArrayList.cpp
    mkdir -p dist/Debug/GNU-Linux-x86
    g++     -o dist/Debug/GNU-Linux-x86/cppapplication_1 build/Debug/GNU-Linux-x86/ArrayList.o build/Debug/GNU-Linux-x86/main.o  
    build/Debug/GNU-Linux-x86/main.o: In function `main':
    /home/sam/NetBeansProjects/CppApplication_1/main.cpp:16: undefined reference to `ArrayList<double>::ArrayList(int)'
    collect2: ld returned 1 exit status
    make[2]: *** [dist/Debug/GNU-Linux-x86/cppapplication_1] Fout 1
    make[2]: Map '/home/sam/NetBeansProjects/CppApplication_1' wordt verlaten
    make[1]: *** [.build-conf] Fout 2
    make[1]: Map '/home/sam/NetBeansProjects/CppApplication_1' wordt verlaten
    make: *** [.build-impl] Fout 2
    BUILD FAILED (exit value 2, total time: 180ms)
    no votes  

  2. #2
    Tyfius's Avatar
    Registered
    01/09/02
    Location
    Peutie
    Posts
    7,664
    iTrader
    0
    Mentioned
    4 Post(s)
    Reputation
    13/105
    De implementatie van uw template functies moet zich in dezelfde file bevinden als de declaratie. Meer informatie op [35] Templates Updated! , C++ FAQ Lite
    Vanaf nu gaan we verder op BeyondGaming!
    In deze thread wordt uitgelegd hoe je jouw account kan migreren.
    no votes  

  3. #3
    Curahee Q's Avatar
    Registered
    07/12/07
    Location
    Hoogstraten
    Posts
    854
    iTrader
    0
    Mentioned
    0 Post(s)
    Bedankt, heb nu zowel mijn implementatie als declaratie in ArrayList.h gestoken. Bedankt ook voor de link.
    no votes  

  4. #4
    forloRn_'s Avatar
    Registered
    23/11/03
    Location
    Landeurp
    Posts
    1,791
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    10/17
    Tips: een default argument zetten in je constructor en je array deleten.
    no votes  

  5. #5
    Curahee Q's Avatar
    Registered
    07/12/07
    Location
    Hoogstraten
    Posts
    854
    iTrader
    0
    Mentioned
    0 Post(s)
    Wat bedoel je juist forloRn_?

    Als ik dit doe
    Code:
    template <class T>
    ArrayList<T>::ArrayList(int capacity=10) {
        if(capacity <= 0) {
            throw "Capaciteit moet groter zijn dan 0!";
        }
    
        m_capacity = capacity;
        m_size = 0;
    
        data = new T[m_capacity];
    }
    Kan ik deze niet aanroepen met

    ArrayList<string> l;

    Array deleten gebeurd in de destructor die ondertussen wel is geprogrammeerd.
    no votes  

  6. #6
    Tyfius's Avatar
    Registered
    01/09/02
    Location
    Peutie
    Posts
    7,664
    iTrader
    0
    Mentioned
    4 Post(s)
    Reputation
    13/105
    Quote Originally Posted by Nocturn View Post
    This quote is hidden because you are ignoring this member. Show
    Wat bedoel je met "moet"?

    Ik snap ook wel de realiteit van wat je zegt, maar het is niet omdat ik maar weet heb van slechts 1 c++ compiler die de standaard op deze manier volgt dat je kunt concluderen dat het "moet". :-/
    Moeten is een groot woord. Alternatieve oplossingen worden op die FAQ pagina ook gegeven, maar ik heb al ondervonden dat het meestal gewoon handiger is van die in uw header file te doen.
    Vanaf nu gaan we verder op BeyondGaming!
    In deze thread wordt uitgelegd hoe je jouw account kan migreren.
    no votes  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in