O
oxforty
The following produce a LINK2019: Unresolved externals. I was banging
my head a while to figure out why. I have the static member variable
of class A declared in global scope outside of main(), This is
happening only if I have the files in seperate source files. If I cram
them into one, everything is fine and dandy.
Environment: VS2003, WinXP
ServerMap.h
=========
#ifndef SERVERMAP_H
#define SERVERMAP_H
template<class T>
class ServerMap
{
public:
ServerMap();
~ServerMap();
T operator [] (int index);
int size();
private:
enum {SIZE = 100};
T stack[SIZE];
int sz; //current index
};
#endif
ServerMap.cpp
============
#include "servermap.h"
template <class T>
ServerMap<T>::ServerMap()
{
sz = 0;
}
template <class T>
ServerMap<T>::~ServerMap()
{
}
template <class T>
T ServerMap<T>:
perator [] (int index)
{
return stack[index];
}
template <class T>
int ServerMap<T>::size()
{
return sz;
}
#include <iostream>
#include <string>
#include "servermap.h"
class A
{
public:
static ServerMap<std::string> mapInfSrv;
};
ServerMap<std::string> A::mapInfSrv;
int main()
{
A::mapInfSrv.size();
return 0;
}
This is what the compile error looks like.
Error produced :
templates.obj : error LNK2019: unresolved external symbol "public: int
__thiscall ServerMap<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::size(void)" (?
size@?$ServerMap@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@@QAEHXZ) referenced in function _main
templates.obj : error LNK2019: unresolved external symbol "public:
__thiscall ServerMap<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::ServerMap<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >(void)" (??0?$ServerMap@V?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in
function _$E1
templates.obj : error LNK2019: unresolved external symbol "public:
__thiscall ServerMap<class std::basic_string<char,struct
$ServerMap@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@@QAE@XZ) referenced in function _$E2
Debug/templates.exe : fatal error LNK1120: 3 unresolved externals
Any help is appreciated
my head a while to figure out why. I have the static member variable
of class A declared in global scope outside of main(), This is
happening only if I have the files in seperate source files. If I cram
them into one, everything is fine and dandy.
Environment: VS2003, WinXP
ServerMap.h
=========
#ifndef SERVERMAP_H
#define SERVERMAP_H
template<class T>
class ServerMap
{
public:
ServerMap();
~ServerMap();
T operator [] (int index);
int size();
private:
enum {SIZE = 100};
T stack[SIZE];
int sz; //current index
};
#endif
ServerMap.cpp
============
#include "servermap.h"
template <class T>
ServerMap<T>::ServerMap()
{
sz = 0;
}
template <class T>
ServerMap<T>::~ServerMap()
{
}
template <class T>
T ServerMap<T>:
{
return stack[index];
}
template <class T>
int ServerMap<T>::size()
{
return sz;
}
#include <iostream>
#include <string>
#include "servermap.h"
class A
{
public:
static ServerMap<std::string> mapInfSrv;
};
ServerMap<std::string> A::mapInfSrv;
int main()
{
A::mapInfSrv.size();
return 0;
}
This is what the compile error looks like.
Error produced :
templates.obj : error LNK2019: unresolved external symbol "public: int
__thiscall ServerMap<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::size(void)" (?
size@?$ServerMap@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@@QAEHXZ) referenced in function _main
templates.obj : error LNK2019: unresolved external symbol "public:
__thiscall ServerMap<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::ServerMap<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >(void)" (??0?$ServerMap@V?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in
function _$E1
templates.obj : error LNK2019: unresolved external symbol "public:
__thiscall ServerMap<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >(void)" (??1?std::char_traits said:::~ServerMap<class std::basic_string<char,struct
$ServerMap@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@@QAE@XZ) referenced in function _$E2
Debug/templates.exe : fatal error LNK1120: 3 unresolved externals
Any help is appreciated