Sign in to follow this  
DimitriA

[c++] Strange error - Unresolved External @ Link time w/ Template

Recommended Posts

DimitriA    100
Hey all I am experiencing a very strange error, compliler says

Error 1 error LNK2019: unresolved external symbol "private: void __thiscall Vec<int>::create(void)" (?create@?$Vec@H@@AAEXXZ) referenced in function "public: __thiscall Vec<int>::Vec<int>(void)" (??0?$Vec@H@@QAE@XZ) c:\practice\vec\vec\main.obj vec


and here is my source code

vec.h

#ifndef GUARD_vec_h
#define GUARD_vec_h

#include <memory>
#include <allocators>

template <class T> class Vec {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T& ref;
typedef const T& const_ref;

Vec() { create(); }
explicit Vec(size_type n, const T& t= T()) { create(n,t); }
~Vec() { uncreate(); }
Vec(const Vec& v) { create(v.begin(), v.end()); }

size_type size() const { return avail - data; }
void push_back(const T& val) {
if(avail == limit) grow();
unchecked_append(val);
}
//operators
T& operator[](size_type i) { return data[i]; }
Vec& operator=(const Vec&);

const T& operator[] (size_type i) const {return data[i]; }

iterator begin() { return data; }
const_iterator begin() const { return data; }

iterator end() { return limit; }
const_iterator end() const { return limit; }
private:
void create();
void create(size_type, const T&);
void create(const_iterator, const_iterator);

iterator data;
iterator avail;
iterator limit;

std::allocator<T> alloc;



void uncreate();

void grow();
void unchecked_append(const T&);
};


#endif





vec.cpp

#include "vec.h"

template <class T>
Vec<T>& Vec<T>::operator=(const Vec& rhs) {
if(&rhs != this) {
uncreate();

create(rhs.begin(), rhs.end());
}
return *this;
}

template <class T>
void Vec<T>::create() { data = avail = limit = 0; }

template <class T>
void Vec<T>::create(size_type n, const T& val) {
data = alloc.allocate(n);
limit = avail = data + n;
std::unitialized_fill(data,limimt,val);
}

template <class T>
void Vec<T>::create(const_iterator i, const_iterator j) {
data = alloc.allocate(j-i);
limit = avail = std::unitialized_copy(i,j,data);
}

template <class T>
void Vec<T>::uncreate() {
if(data) {
iterator it = avail;

while(it != data)
alloc.destroy(--it);

alloc.deallocate(data,limit-data);
}
data = limit = avail = 0;
}

template <class T>
void Vec<T>::grow() {
size_type new_size = max(2* (limit - data), ptrdiff_t(1));

iterator new_data = alloc.allocate(new_size);
iterator new_avail = std::uninitialized_copy(data,avail,new_data);

uncreate();

data = new_data;
avail = new_avail;

limit = data + new_size;
}

template <class T>
void Vec<T>::unchecked_append(const T& val) {
alloc.construct(avail++,val);
}




and just the entry point


Any clue why I am getting unresolved linking issues?

[Edited by - DimitriA on October 17, 2010 3:28:03 AM]

Share this post


Link to post
Share on other sites
Waaayoff    952
Using templates across two files creates linker errors. If you want to use templates, you have to move all function definitions back together with the declarations.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this