简体   繁体   中英

extern for class instance with template

I have the following instance of a class with template in main.cpp

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

mypair <int> myobject (100, 75);

I wish to have a header file that has an extern to this class instance, ie "myobject".

How can this be achieved?

I already tried:

* extern mypair<int> myobject;
* extern template mypair<int> myobject;
* extern template class mypair<int> myobject;

You need to have the template class defined in a header which must be included before the extern declaration.

If your compiler encounters a header which says extern mypair<int> myobject; it has no clue what kind of entity mypair is. You must first define it - through including the header containing the template class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM