DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: declaring member fn of class template outside does not work! :((

  1. #1
    Join Date
    May 2008
    Posts
    3

    Angry declaring member fn of class template outside does not work! :((

    hi i am unable to declare member functions of a template class outside it. what i am doing is something simple on VC++ 2008 express edition.
    In a header file the following
    Code:
    template <class T> class Queue
    {
    public:
    	Queue(const int &int_size, Logger *ptr_LOGGER);
    	~Queue();
           // ..............
    };
    
    template <class T> class FrameQ:public Queue<T>
    {
    public:
      FrameQ(const int &int_size, const Logger *ptr_LOGGER);
           // ..............
    };
    In the corresp c++ file the member function delarations for the class templates
    Code:
    template<class T> Queue<T>::Queue(const int &int_size, Logger *ptr_LOGGER):
    {
    //...................
    }
    
    template <class T> FrameQ<T>::FrameQ(const int &int_size, const Logger *ptr_LOGGER):Queue<T>(int_size, ptr_LOGGER)
    {
    //...................
    }
    okay so i have another cpp file and i compile it and there is no problem. but the $hit happens when i add this in the main and link it
    Code:
    int main(int argc, char **argv)
    {
    FrameQ<int>* frameQ = new FrameQ<int>(5, NULL);
    delete frameQ ;
    return 1;
    }
    The linker error i get is
    error LNK2019: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(int const &,class Logger *)"
    similarly i get error for the destructors and any function i attempt to access using frameQ ->fn()

    what is the problem here? i thought it was to do with my dated vc++ 2003 complier, so i went and installed vc++ 2008 express and spent around 3hours compiling my project libs. but still the same @!$&*#% problem

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    the functions for which you get a linker error are only declared in the header file. they are defined in the implementation file (.cpp/.cc).

    the pedantic answer would be: to use templates without their definition being visible, they must be exported. ie. in the .cpp, they must be defined this way:
    Code:
    #include "header file"
    export template<class T> 
    Queue<T>::Queue(const int &int_size, Logger *ptr_LOGGER) // ... 
    {
         //...................
    }
    // etc
    however, most current compilers do not support the export keyword. ( Comeau C++ being the notable exception)

    a practical solution would be: move the definitions from the .cpp to the header file (and throw the now empty .cpp file away).

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Intel's compiler also supports export but it doesn't admit that. This is an undocumented feature. They probably weren't that happy with how their export is implemented so they decided to turn it off by default. Practically, export is dead. So the only viable approach is the inclusion model whereby the definition of every template is included in every translation unit that uses that template.
    Danny Kalev

Similar Threads

  1. Object as Member of another class
    By Peter_APIIT in forum C++
    Replies: 7
    Last Post: 08-12-2007, 01:20 AM
  2. help in account class
    By kakashi in forum C++
    Replies: 1
    Last Post: 04-20-2007, 02:12 PM
  3. Replies: 1
    Last Post: 12-04-2005, 05:27 PM
  4. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08:53 PM
  5. Problems getting template match=.. to work
    By Neal Andrews in forum XML
    Replies: 1
    Last Post: 02-09-2001, 12:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center