CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    how to export class with template

    I have the following class header in library, but when i initialize in main the class, i have an error unresolved external symbols. So the class is not exported as it should.

    Code:
    template <typename Key, typename Value>
    class UTILITIES_EXPORT MyMap : public QMap<Key, Value>
    {
    public:
    	MyMap() : QMap<Key, Value>() { }
    	
    	MyMap<Key, Value>& operator()(const Key & key, const Value & value)
    	{
    		this->insert(key, value);
    		return *this;
    	}
    };

    Code:
    MyMap<int,int> mm; //error unresolved external symbols here

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how to export class with template

    *What* symbol is "unresolved"?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: how to export class with template

    Is UTILITIES_EXPORT in your program defined correctly?

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: how to export class with template

    If you have templates you EITHER.

    1) Have ALL of the code for your template class defined in a header so the compiler can instantiate the desired specialisation in-situ. as desired. In this case, you don't even need a .lib/.dll at all, for the template per say, though the class functions may refer to functions in a lib.

    2) you have code in a set of .cpp's compiled into your .lib/.dll. In this case your lib/dll needs to explicitely instantiate ALL the specializations you will need in the consumers of your lib.


    the thing to remember here is that templates aren't doing anything at all until the moment where the compiler sees you are USING a specific template specialization, it then essentially "silently creates an extra .cpp and .h for that specific specialization and includes that in your project and compiles it alongside the rest of your code".
    This is not how it really works, but this simplification may make it clearer how/why things are the way they are. The truth is far more complex and convoluted
    This is why if some of your code is in a .cpp, that the linker may not find certain functions, because you didn't stick 'em in the lib, and the compiler couldn't add them into that "hidden .cpp" file either because it can't see the C++ code for how to create that.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: how to export class with template

    it's unclear if the code you gave is complete to generate the linkererror, but if yes, than that define probably forces the linker to try and find the MyMap<int,int> specialisation in the lib, but the lib didn't instantiate that specialization.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured