user defined template

This is a discussion on user defined template within the C++ Programming forums, part of the General Programming Boards category; hello, i am trying to build a Queue, with the use of templates, so that it is possible to create ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    user defined template

    hello, i am trying to build a Queue, with the use of templates, so that it is possible to create 'int', 'string' queues and so on...

    i have been given a class definition for my template queue (TQueue), which includes this:
    void add(Node<T> *n);

    i have created a my queue like this:
    TQueue<int> myQueue(); //works fine!

    my problem is when i am to add Nodes, i do this:
    int number=10;
    Node<int>* myIntNode=new Node<int>(number);

    myQueue.add(myIntNode); //the compiler doesn't accept this

    regarding my last operation, i get the following error msg:
    "Structure required on left side of . or .*"

    what am i doing wrong?

    anyone?
    I use BorlandBuilder 5

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I can't see anything wrong with your add function from the info that you've given, but you'll need to change -

    TQueue<int> myQueue();

    to

    TQueue<int> myQueue;

    C++ has difficulty differentiating between construction and function declaration.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    if i don't use the brackets i get the following linker error:
    [Linker Error] Unresolved external 'TQueue<int>::~TQueue<int>()' referenced from C:\PROGRAMMING\C++\EXERCISE V\DRIVER.OBJ

    my implementation looks like this:

    template<class T>TQueue<T>::TQueue()
    {
    head=tail=current=NULL;
    }
    template<class T>TQueue<T>::~TQueue()
    {
    //not done yet!
    }
    template<class T> void TQueue<T>::add(Node<T> *n)
    {
    if(head==NULL)
    {
    head=tail=current=n;
    head->link(NULL);
    }
    else
    {
    current=tail;
    current->link(n);
    tail=current->link();
    }
    }

    plus some more, but this is out of context regarding my problem.

    see anything wrong. i normally get linker errors if i forget to declare constructors, but i cannot see anything wrong with my constructor (?).

    thanks in advance
    I use BorlandBuilder 5

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Looks Ok, is the implemenation in the header file?

  5. #5
    Unregistered
    Guest
    no, my implementation is in a cpp file ( i have a separate header file).

    i don't seem to get this!

    xagiber


    btw, i use borland builder5

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You cannot have templates split into source and header. You will ave to put it all into the header.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    THAT was my problem! thanks a lot!
    I use BorlandBuilder 5

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It is possible to split template declarations and implementations.
    You can do this....

    Templates.h

    // your template declarations go here

    // then one of those rarae times you will need to do this...
    #include "Templates.cpp"

    Templates.cpp

    //Your templates implementations go here....

    2 different files. Yet the compiler sees them as one!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    unregistered
    Guest
    Hi stoned-coder

    Thanks for that - I was getting a linker error - undefined
    sac<int>::sac(int) from main() // sac being the template

    Didn't realize that template declaration and implementation had to be together (using Dev-C++)

    Thought they'd work just like ordinary classes

    But...

    What if you have say two separate files eg main.cpp and other.cpp and they both use the same template class 'tclass' ?

    Does #including tclass.h in both cause a problem ?

    I have heard that MVC++ also requires that dec and imp have to be together - anyone know how Mickeysoft suggest structuring code around the problem ?

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Does #including tclass.h in both cause a problem ?

    No you should be alright with templates. Another alternative is to #inlude the cpp at the bottom of your header file. Then you can just include the header file. However, if you want other people to be able to use your class you'll still have to include all the files and can't hide the source in a library, so the most common method is to write the implementation in the header file.

    >anyone know how Mickeysoft suggest structuring code around the problem ?

    The same way as all the other compilers that don't support the export keyword.

  11. #11
    Unregistered
    Guest
    Thanks Sorensen

    I found a similar answer in a serach

    //tclass.h

    #ifndef tclass_h
    #define tclass_hh

    template <class blah...

    #include tclass.cpp

    #endif

    //tclass.cpp

    #ifndef tclass_cpp
    #define tclass_cpp

    // imp code

    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined template question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2006, 05:01 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 06:36 PM
  4. win32 and user defined classes
    By paulf in forum Windows Programming
    Replies: 4
    Last Post: 04-16-2002, 06:12 PM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

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