templates and linking problems

Discussion in 'C++' started by david, Apr 15, 2008.

  1. david

    david Guest

    Hello, I decided to play a bit with templates and classes, but
    discovered some problems.
    So, I have 3 files: LinkedList.h LinkedList.cpp and test.cpp (by the
    way it is not finished fully)

    LinkedList.h

    #ifndef LinkedList_byDavid
    #define LinkedList_byDavid

    template <class lType>
    class LinkedList {
    public:
    LinkedList() : lSize(0) {};
    void add(lType item);
    lType get(int num) const;
    int Size() const;
    //~LinkedList();

    private:
    typedef struct child {
    lType *value;
    child *next;
    } child;
    child *root;
    int lSize;
    };

    #endif

    LinkedList.cpp

    #include <cstring>
    #include "LinkedList.h"

    template <class lType>
    int LinkedList<lType>::Size() const {
    return lSize;
    }

    template <class lType>
    void LinkedList<lType>::add(lType item) {
    child *newChild = new child;
    newChild->value = new lType;
    newChild->value = item;
    newChild->next = root;
    root = newChild;
    lSize++;
    }

    and test.cpp:

    #include <iostream>
    #include <cstring>
    #include "LinkedList.h"

    using namespace std;

    int main (int argc, char const *argv[])
    {
    cout << "Bandymas" << endl;
    LinkedList<int> as;
    LinkedList<int> *tu = new LinkedList<int>();
    as.add(5);
    as.add(5);
    as.add(5);
    as.add(5);
    as.add(5);
    int kazkas = as.Size();
    cout << "dydis: " << kazkas << endl;
    cout << (*tu).Size() << endl;

    return 0;
    }

    I use this to compile and link:
    g++ -Wall -ansi -pedantic -c LinkedList.cpp
    g++ -Wall -ansi -pedantic -c test.cpp
    g++ -o test test.o linkedlist.o

    And getting:
    Undefined symbols:
    "LinkedList<int>::add(int)", referenced from:
    _main in test.o
    _main in test.o
    _main in test.o
    _main in test.o
    _main in test.o
    "LinkedList<int>::Size() const", referenced from:
    _main in test.o
    _main in test.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    on gcc version 4.0.1 (Apple Inc. build 5465)

    There could be two problems:
    a) declarations does not match, but as I look it looks that everything
    is okay.
    b) something wrong with LinkedList.cpp, maybe I missed something.

    Any tips what I have missed here?
    david, Apr 15, 2008
    #1
    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. Tobias Langner

    linking templates problem

    Tobias Langner, Jul 30, 2003, in forum: C++
    Replies:
    1
    Views:
    361
    Victor Bazarov
    Jul 30, 2003
  2. murali
    Replies:
    1
    Views:
    288
    John Harrison
    Sep 2, 2003
  3. JKop
    Replies:
    3
    Views:
    455
  4. recover
    Replies:
    2
    Views:
    789
    recover
    Jul 25, 2006
  5. Replies:
    3
    Views:
    293
    siddhu
    May 7, 2007
Loading...

Share This Page