Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,
Follows are my codes:
XML
#pragma once

// Disable warnings about long names
#ifdef WIN32
  #pragma warning( disable : 4786)
#endif

#include <map>

using namespace std;

template <typename _Ty> class IMagicMap
{
public:
    _Ty m_BaseMap;

    virtual bool Add(_Ty& NewObject, LPCTSTR lpszKeyName = NULL) = 0;
    virtual bool Remove(LPCTSTR lpszKeyName) = 0;
    virtual bool Find(LPCTSTR lpszKeyName, _Ty& Found) = 0;

protected:
    virtual ~IMagicMap(){}
};

template <typename _Ty> class CMagicMap : public IMagicMap<_Ty>
{
public:
    CMagicMap();
    virtual ~CMagicMap();

    map<CString, _Ty> m_BaseMaps;
    typename map<CString, _Ty>::const_iterator m_itBaseMap;

    bool Add(_Ty& NewObject, LPCTSTR lpszKeyName = NULL);
    bool Remove(LPCTSTR lpszKeyName);
    bool Find(LPCTSTR lpszKeyName, _Ty& Found);
};


XML
#include "StdAfx.h"
#include "MagicMap.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

template <typename _Ty>
CMagicMap<_Ty>::CMagicMap()
{

};

template <typename _Ty>
CMagicMap<_Ty>::~CMagicMap()
{
    // Garbage collector
    for (m_itBaseMap = m_BaseMaps.begin(); m_itBaseMap != m_BaseMaps.end();)
    {
        m_BaseMaps.erase(m_itBaseMap++);
    }
}

template <typename _Ty>
bool CMagicMap<_Ty>::Add(_Ty& NewObject, LPCTSTR lpszKeyName/* = NULL*/)
{
    m_BaseMap = NewObject;
    CString strKeyName = _T("");

    if (NULL != lpszKeyName)
       strKeyName = lpszKeyName;
    else
       strKeyName = NewObj.GetChannelName();

    if ( strKeyName.IsEmpty() )
       return false;

    m_BaseMaps[strKeyName] = NewObject;

    return true;
}

template <typename _Ty>
bool CMagicMap<_Ty>::Remove(LPCTSTR lpszKeyName)
{
    if (NULL == lpszKeyName)
       return false;

    bool bRes;

    int nResult = m_BaseMaps.erase( lpszKeyName );

    if ( nResult != 0 )
       bRes = true;
    else
       bRes = false;

    return bRes;
}

template <typename _Ty>
bool CMagicMap<_Ty>::Find(LPCTSTR lpszKeyName, _Ty& Found)
{
    if ( (NULL == lpszKeyName) )
       return false;

    bool bRet;
    m_itBaseMap = m_BaseMaps.find(lpszKeyName);

    if ( m_itBaseMap != m_BaseMaps.end() )
    {
       Found = m_itBaseMap->second;
       bRet = true;
    }
    else
       bRet = false;

    return bRet;
}


XML
#pragma once

#include "MagicMap.h"
#include "Annotation.h"
#include "ColorBar.h"
#include "GridLine.h"
#include "Logo.h"
#include "MapInfo.h"

class CBaseMaps
{
public:
    CBaseMaps();
    CBaseMaps(const CBaseMaps& rhs);
    virtual ~CBaseMaps();

    const CMagicMap<CAnnotation>& Annotations() const;
    const CMagicMap<CColorBar>& ColorBars() const;
    const CMagicMap<CGridLine>& GridLines() const;
    const CMagicMap<CLogo>& Logos() const;
    const CMagicMap<CMapInfo>& MapInfos() const;

    void Annotations(CMagicMap<CAnnotation>& bmAnnotations);
    void ColorBars(CMagicMap<CColorBar>& bmColorBars);
    void GridLines(CMagicMap<CGridLine>& bmGridLines);
    void Logos(CMagicMap<CLogo>& bmLogos);
    void MapInfos(CMagicMap<CMapInfo>& bmMapInfos);

// Operators
    const CBaseMaps & operator = (const CBaseMaps& rhs);
    bool operator<( const CBaseMaps& BaseMaps ) const;

protected:
// Attributes
    CMagicMap<CAnnotation> m_bmAnnotations;
    CMagicMap<CColorBar>   m_bmColorBars;
    CMagicMap<CGridLine>   m_bmGridLines;
    CMagicMap<CLogo>       m_bmLogos;
    CMagicMap<CMapInfo>    m_bmMapInfos;
};

#include "StdAfx.h"
#include "BaseMaps.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBaseMaps::CBaseMaps()
{

}

CBaseMaps::CBaseMaps( const CBaseMaps &rhs )
{
    this->m_bmAnnotations = rhs.m_bmAnnotations;
    this->m_bmColorBars   = rhs.m_bmColorBars;
    this->m_bmGridLines   = rhs.m_bmGridLines;
    this->m_bmLogos       = rhs.m_bmLogos;
    this->m_bmMapInfos    = rhs.m_bmMapInfos;
};

CBaseMaps::~CBaseMaps()
{

}

const CBaseMaps& CBaseMaps::operator = (const CBaseMaps& rhs)
{
    if (&rhs != this)
    {
       this->m_bmAnnotations = rhs.m_bmAnnotations;
       this->m_bmColorBars   = rhs.m_bmColorBars;
       this->m_bmGridLines   = rhs.m_bmGridLines;
       this->m_bmLogos       = rhs.m_bmLogos;
       this->m_bmMapInfos    = rhs.m_bmMapInfos;
    }

    return (*this);
}

bool CBaseMaps::operator<( const CBaseMaps& BaseMaps ) const
{
    return true;
};

const CMagicMap<CAnnotation>& CBaseMaps::Annotations() const
{
    return m_bmAnnotations;
}

const CMagicMap<CColorBar>& CBaseMaps::ColorBars() const
{
    return m_bmColorBars;
}

const CMagicMap<CGridLine>& CBaseMaps::GridLines() const
{
    return m_bmGridLines;
}

const CMagicMap<CLogo>& CBaseMaps::Logos() const
{
    return m_bmLogos;
}

const CMagicMap<CMapInfo>& CBaseMaps::MapInfos() const
{
    return m_bmMapInfos;
}

void CBaseMaps::Annotations(CMagicMap<CAnnotation>& bmAnnotations)
{
    m_bmAnnotations = bmAnnotations;
}

void CBaseMaps::ColorBars(CMagicMap<CColorBar>& bmColorBars)
{
    m_bmColorBars = bmColorBars;
}

void CBaseMaps::GridLines(CMagicMap<CGridLine>& bmGridLines)
{
    m_bmGridLines = bmGridLines;
}

void CBaseMaps::Logos(CMagicMap<CLogo>& bmLogos)
{
    m_bmLogos = bmLogos;
}

void CBaseMaps::MapInfos(CMagicMap<CMapInfo>& bmMapInfos)
{
    m_bmMapInfos = bmMapInfos;
}


When linking with VS2008, the linker throwed the following error to me:

Compiling...
1>BaseMaps.cpp
1>Linking...
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CMagicMap<class CLogo>::~CMagicMap<class CLogo>(void)" (??1?$CMagicMap@VCLogo@@@@UAE@XZ) referenced in function __unwindfunclet$??0CBaseMaps@@QAE@XZ$0
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CMagicMap<class CGridLine>::~CMagicMap<class CGridLine>(void)" (??1?$CMagicMap@VCGridLine@@@@UAE@XZ) referenced in function __unwindfunclet$??0CBaseMaps@@QAE@XZ$0
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CMagicMap<class CColorBar>::~CMagicMap<class CColorBar>(void)" (??1?$CMagicMap@VCColorBar@@@@UAE@XZ) referenced in function __unwindfunclet$??0CBaseMaps@@QAE@XZ$0
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CMagicMap<class CAnnotation>::~CMagicMap<class CAnnotation>(void)" (??1?$CMagicMap@VCAnnotation@@@@UAE@XZ) referenced in function __unwindfunclet$??0CBaseMaps@@QAE@XZ$0
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: __thiscall CMagicMap<class CMapInfo>::CMagicMap<class CMapInfo>(void)" (??0?$CMagicMap@VCMapInfo@@@@QAE@XZ) referenced in function "public: __thiscall CBaseMaps::CBaseMaps(void)" (??0CBaseMaps@@QAE@XZ)
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: __thiscall CMagicMap<class CLogo>::CMagicMap<class CLogo>(void)" (??0?$CMagicMap@VCLogo@@@@QAE@XZ) referenced in function "public: __thiscall CBaseMaps::CBaseMaps(void)" (??0CBaseMaps@@QAE@XZ)
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: __thiscall CMagicMap<class CGridLine>::CMagicMap<class CGridLine>(void)" (??0?$CMagicMap@VCGridLine@@@@QAE@XZ) referenced in function "public: __thiscall CBaseMaps::CBaseMaps(void)" (??0CBaseMaps@@QAE@XZ)
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: __thiscall CMagicMap<class CColorBar>::CMagicMap<class CColorBar>(void)" (??0?$CMagicMap@VCColorBar@@@@QAE@XZ) referenced in function "public: __thiscall CBaseMaps::CBaseMaps(void)" (??0CBaseMaps@@QAE@XZ)
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: __thiscall CMagicMap<class CAnnotation>::CMagicMap<class CAnnotation>(void)" (??0?$CMagicMap@VCAnnotation@@@@QAE@XZ) referenced in function "public: __thiscall CBaseMaps::CBaseMaps(void)" (??0CBaseMaps@@QAE@XZ)
1>BaseMaps.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CMagicMap<class CMapInfo>::~CMagicMap<class CMapInfo>(void)" (??1?$CMagicMap@VCMapInfo@@@@UAE@XZ) referenced in function __unwindfunclet$??0CBaseMaps@@QAE@ABV0@@Z$0
1>D:\SetTest\Debug\SetTest.exe : fatal error LNK1120: 10 unresolved externals
1>Build log was saved at "file://d:\SetTest\SetTest\Debug\BuildLog.htm"
1>SetTest - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Could you help me to resolve these "unresolved externals" problems.

Thank you very much!

Golden Lee
Posted
Updated 22-Dec-10 1:54am
v3
Comments
ShilpiP 22-Dec-10 0:44am    
Please specify which symbol is unresolved or update question with error message that you receive.
Richard MacCutchan 22-Dec-10 7:51am    
Edited so the diagnostic information is visible; I do wish people would review their posted questions rather than just pressing submit and assuming it's readable!

1 solution

Template declarations and definitions should be in the header file. We cannot split it between .h and .cpp files. You can check if that is the issue.
 
Share this answer
 
Comments
Emilio Garavaglia 22-Dec-10 8:31am    
Dear Rejeesh.T.S
I merge the template declarations and definitions into the header file. My program passed VS2008 linking. But VS 6.0 can link the splitted one with no errors. I don't why.

Thank you very much!

Golden Lee

(edited by ...)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900