custom vector class C++ -


this question has answer here:

i making own vector class.

.h:

template<typename t> class myvector { private:     t *elements;     int elementcount; public:     myvector();     myvector(int size);     void push_back(t value);     void pop_back();     int size();     t at(int index);     bool empty();     void clear();     void swap(myvector v2); }; 

.cpp:

template<typename t> myvector<t>::myvector() {     elementcount = 0;     elements = new int[elementcount];     elements = (int *) realloc (elements, elementcount * sizeof(int)); } 

main.cpp:

#include "myvector.h"  int main() {     myvector<char> mytestvector;        return 0; } 

i getting error when trying create myvector object, error :

myvector::myvector(), referenced from: _main in main.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation)

i wrote class work specified type, need work given type.

why getting error? in advance!

c++ templates must have definitions in header file. because compiler generates object files every instanced type @ compile time. if move declarations .h file, code should link ok.

see here decent overview on how/why templates work.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -