LNK 2019 problem
May 11, 2013 at 12:00am UTC
I'm trying to implement Vector with templates but, I've got a problem with linker. What did I wrong ? Does anybody can see some mistakes?
I enclose all of my important files.
compilation communicate:
1> vector.cpp
1> main.cpp
1> Generating Code...
1> Compiling...
1> city.cpp
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> path.cpp
1> operators.cpp
1> list.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vector<struct dat>::~vector<struct dat>(void)" (??1?$vector@Udat@@@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: struct dat & __thiscall vector<struct dat>::operator[](unsigned int)" (??A?$vector@Udat@@@@QAEAAUdat@@I@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall vector<struct dat>::push_back(struct dat const &)" (?push_back@?$vector@Udat@@@@QAEXABUdat@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vector<struct dat>::vector<struct dat>(int)" (??0?$vector@Udat@@@@QAE@H@Z) referenced in function _main
1>C:\Users\USER\documents\visual studio 2010\Projects\mapa\Debug\mapa.exe : fatal error LNK1120: 4 unresolved externals
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <stdlib.h>
#include "structs.h"
#include "vector.h"
#include "list.h"
#include "city.h"
#include "operators.h"
int main(){
position rozmiar;
char **mapa;
dat data;
vector<dat> miasta = vector<dat>( 10 );
std::cin >> rozmiar.w >> rozmiar.h;
mapa = new char *[rozmiar.h];
for ( int i = 0; i < rozmiar.h; i++ ){
mapa[i] = new char [rozmiar.w];
for ( int j = 0; j < rozmiar.w; j++){
std::cin >> mapa[i][j];
if ( mapa[i][j] == '*' ){
data.p.h = i;
data.p.w = j;
miasta.push_back( data );
};
};
};
for ( int i = 0; i < miasta.size; i++ ){
miasta[i].c = findName( miasta[i].p, mapa );
};
miasta.~vector();
for ( int i = 0; i < rozmiar.h; i++ ){
delete mapa[i];
};
delete mapa;
return 0;
};
vector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef VECTOR_GUARD
#define VECTOR_GUARD
template <class T>
class vector{
public :
T* buffer;
int size;
int capacity;
vector(void );
vector(int size);
~vector(void );
void push_back(const T & data);
void pop_back(void );
void reserve(int capacity);
void resize(int size);
T & operator [](unsigned int index);
vector<T> & operator = (const vector<T> & v);
};
#endif
vector.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <stdlib.h>
#include "vector.h"
template <class T>
vector<T>::vector(void ){
capacity = 10;
size = 0;
buffer = new T[capacity];
}
template <class T>
vector<T>::vector(int size){
capacity = size;
size = size;
buffer = new T[size];
}
template <class T>
vector<T> & vector<T>::operator = (const vector<T> & v){
delete [ ] buffer;
size = v.size;
capacity = v.capacity;
buffer = new dat[size];
for (int i = 0; i < size; i++)
buffer[i] = v.buffer[i];
return *this ;
}
template <class T>
void vector<T>::push_back(const T & data){
if (size >= capacity)
reserve(capacity +5);
buffer [ size++ ] = data;
}
template <class T>
void vector<T>::pop_back(void ){
size--;
}
template <class T>
void vector<T>::reserve( int new_capacity){
if (buffer == NULL){
size = 0;
capacity = 0;
}
T* tmp = new T[capacity];
for ( int i = 0; i < size; i++ ){
tmp[i] = buffer[i];
};
capacity = new_capacity;
delete [] buffer;
buffer = tmp;
}
template <class T>
T & vector<T>::operator [](unsigned int index){
return buffer[index];
}
template <class T>
vector<T>::~vector(void ){
delete [] buffer;
}
structs.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef STRUCTS_GUARD
#define STRUCTS_GUARD
#define NAMESIZE 32
template <class T> class list;
struct cityName{
char n[NAMESIZE];
int length;
};
struct position{
int h;
int w;
};
struct neighbour{
cityName c;
int distance;
int index;
};
struct opp{
position* tab;
int size;
};
struct dat{
position p;
cityName c;
list <neighbour>* l;
};
#endif
Last edited on May 11, 2013 at 12:09am UTC
May 11, 2013 at 12:11am UTC
I believe templates must have most (if not, all) methods inlined.
May 11, 2013 at 12:26am UTC
I have never used "inline". Where should I add it ?
I've edited vector.cpp, but it's still not working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <stdlib.h>
#include "vector.h"
template <class T> inline
vector<T>::vector(void ){
capacity = 10;
size = 0;
buffer = new T[capacity];
}
template <class T> inline
vector<T>::vector(int size){
capacity = size;
size = size;
buffer = new T[size];
}
template <class T> inline
vector<T> & vector<T>::operator = (const vector<T> & v){
delete [ ] buffer;
size = v.size;
capacity = v.capacity;
buffer = new dat[size];
for (int i = 0; i < size; i++)
buffer[i] = v.buffer[i];
return *this ;
}
template <class T> inline
void vector<T>::push_back(const T & data){
if (size >= capacity)
reserve(capacity +5);
buffer [ size++ ] = data;
}
template <class T> inline
void vector<T>::pop_back(void ){
size--;
}
template <class T> inline
void vector<T>::reserve( int new_capacity){
if (buffer == NULL){
size = 0;
capacity = 0;
}
T* tmp = new T[capacity];
for ( int i = 0; i < size; i++ ){
tmp[i] = buffer[i];
};
capacity = new_capacity;
delete [] buffer;
buffer = tmp;
}
template <class T> inline
T & vector<T>::operator [](unsigned int index){
return buffer[index];
}
template <class T> inline
vector<T>::~vector(void ){
delete [] buffer;
}
Last edited on May 11, 2013 at 12:30am UTC
May 11, 2013 at 12:48am UTC
Sorry for being so unclear. What I meant was to put the definitions inside the declarations:
1 2 3 4 5 6 7
// .hpp
template <typename T>
class Something
{
public :
void func(); // declaration
};
1 2 3 4 5 6
// .cpp
template <typename T>
void Something::func()
{ // definition
// your code
}
Becomes:
1 2 3 4 5 6 7 8 9 10
// .hpp
template <typename T>
class Something
{
public :
void func()
{
// Your code.
}
};
And you have no .cpp file. The compiler, I believe, needs the definition to deduce the template arguments inside the function.
Topic archived. No new replies allowed.