instantiation of variable required here, but no definition is available
-
Ich verstehe die Warnung:
instantiation of variable required here, but no definition is available
des CLANG-Compilers nicht. In dem Header definiere ich:
```template<typename T> struct dict { static std::map<T, std::string> create_intern_map(); static const std::map<T, std::string> intern_map; }; template<typename T> std::string str(T t) { std::string res; typename std::map<T, std::string>::const_iterator p = dict<T>::intern_map.find(t); if(p != dict<T>::intern_map.end()) res = p->second; return res; } enum class type1 : unsigned { t10, t11, t12, t13 }; enum class type2 : unsigned { t20, t21, t22, t23 }; ```cpp Die statischen Variablen werden on der source instanziert: ```cpp template<> std::map<type1, std::string> dict<type1>::create_intern_map() { static std::map<type1, std::string> m { { type1::t10, "T1.0" }, { type1::t11, "T1.1" }, { type1::t12, "T1.2" }, { type1::t13, "T1.3" } }; return m; } template<> const std::map<type1, std::string> dict<type1>::intern_map = dict<type1>::create_intern_map(); template<> std::map<type2, std::string> dict<type2>::create_intern_map() { static std::map<type2, std::string> m { { type2::t20, "T1.0" }, { type2::t21, "T2.1" }, { type2::t22, "T2.2" }, { type2::t23, "T2.3" } }; return m; } template<> const std::map<type2, std::string> dict<type2>::intern_map = dict<type2>::create_intern_map(); ```cpp Benutzt wird die str Funktion dann so: ```cpp void test() { std::string s1 = str(type1::t11); std::string s2 = str(type2::t21); std::cout << s1 << ';' << s2 << std::endl; } ```cpp Die Warnung kommt in der str-Funktion beim ersten Aufruf von dict<T>::intern_map. Ich bekomme noch den Tip: add an explicit instantiation declaration to suppress this warning if 'dict<type1>::intern_map' is explicitly instantiated in another translation unit aber ich weiß nicht, wie ich das machen kann. Der msvc-Compiler gibt übrigens keine Warnung. Hat jemand eine Idee, wie ich die Warnung los werde? Markus
-
Scheint die Templatestandardantwort zu sein: Templates werden (bis auf wenige Ausnahemen) im Header implementiet.
-
@mdlem In eine Zeile vor den Code
```cpp
in eine Zeile nach dem Code```
. Den Menüpunkt Bearbeiten findest Du hinter dem Drei-Punkte-Menü rechts unter Deinem Beitrag.
-
@manni66 Aber gerade die Spezialisierungen implementiert man nicht im Header.
-
@mdlem sagte in instantiation of variable required here, but no definition is available:
@manni66 Aber gerade die Spezialisierungen implementiert man nicht im Header.
Ja dann musst du den Wrnungstext lesen und eine explicit instantiation declaration hinzufügen.