embedded - C++ Arduino 1.6 on Visual Studio 2013 -
i'm working on arduino, , wanna build small toy piano. practice create own class called map.
#ifndef map_h #define map_h #include "arduino.h" template <class key, class value> class map { public: map(); ~map(); void add(key key, value value); value find(key key); private: key *keys; value *values; int size; bool resize(); }; #endif
in main call normally:
map<char, int> *name_to_freq = new map<char, int>(); name_to_freq->add('c', 262);
but got error when compile.
ccqwtg4w.ltrans0.ltrans.o*: in function main ccqwtg4w.ltrans0.o*: (.text.startup+0x354): undefined reference map<char, int>::map() ccqwtg4w.ltrans0.o*: (.text.startup+0x3de): undefined reference map<char, int>::add(char, int)
definition in .cpp file:
does 1 see before , know how solve it?
template <typename key, typename value> map<key, value>::map() { keys = new key[defaultarraysize]; values = new value[defaultarraysize]; size = defaultarraysize; } template <typename key, typename value> void map< key, value>::add(key key, value value) { (int = 0; < size; i++) { if (!keys[i]) { keys[i] = key; values[i] = value; if (i >= size/2) { while (!resize()); } } } }
you should put template definition header file.
when working in template class, when instantiate compiler create class template argument fed it, in order compiler should access definitions of functions, cannot access them because in source file.
way 1:
template <class key, class value> class map { public: map(); ~map(); void add(key key, value value); value find(key key); private: key *keys; value *values; int size; bool resize(); }; template <typename key, typename value> map<key, value>::map() { keys = new key[defaultarraysize]; values = new value[defaultarraysize]; size = defaultarraysize; } template <typename key, typename value> void map< key, value>::add(key key, value value) { (int = 0; < size; i++) { if (!keys[i]) { keys[i] = key; values[i] = value; if (i >= size/2) { while (!resize()); } } } }
or can directly define them in class (way 2)
template <class key, class value> class map { public: map() { } ~map(); void add(key key, value value) { } value find(key key) { } private: key *keys; value *values; int size; bool resize(); };
Comments
Post a Comment