Template Class problem...

Started by
4 comments, last by kirkd 20 years ago
I apologize for posting all this code, but I see little alternative. This should be a VERY simple application to put together - or so I thought. Then I started messing with templates. Regardless, I get an unresolved external symbol error for each function. Any ideas??

/*	SOMLattice Header
	Robert Kirk Delisle
	23 March 2004

	Purpose:	Defines the SOM Lattice containing the SOMCell nodes.
				Responsible for creation and management of SOMCells
				and training of the network

	Modification History:

*/

#if !defined(SOMLattice_23March2004__INCLUDED)
#define SOMLattice_23March2004__INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <vector>
using namespace std;

#include "DataSet.h"

template<class SOMCellType> class CSOMLattice
{
private:
	
	struct LatticeCell
	{
		long XPos;			//the X-position within the lattice

		long YPos;			//the Y-position within the lattice

		SOMCellType *Cell;	//the actual cell itself

	};

	vector<LatticeCell> m_SOMCells;	//the nodes of the SOM - assumed to be derived from CSOMCell


	long m_lWidth;					//width of the lattice

	long m_lHeight;					//height of the lattice

	long m_lLatticeRadius;			//topological radius of the SOM lattice

	
public:

	CSOMLattice() {};				

	bool Initialize(long Width, long Height, long NumberOfWeights);
		//primary lattice construction routine


	bool Train(DataSet *data, long numIterations);	
		//function to start training the lattice


	~CSOMLattice();
};

#endif
 

/*	SOMLattice Implementation
	Robert Kirk Delisle
	24 March 2004

	Purpose:	Implements the SOM Lattice containing the SOMCell nodes.
				Responsible for creation and management of SOMCells
				and training of the network

	Modification History:

*/

#include "stdafx.h"

#include "SOMLattice.h"

template<class SOMCellType> bool CSOMLattice<SOMCellType>::Initialize(long Width, long Height, long NumberOfWeights)
{
	/*	Robert Kirk DeLisle
		24 March 2004
		
		Purpose:	Constructs the SOM lattice based upon the passed paramenters.

		Parameters:	Width	-	desired width of the lattice
					Height	-	desired height of the lattice

		Return:		none
	*/
	
	bool bSuccess=true;  //optimistic return

	LatticeCell *currentCell;

	long i,j;	//loop indices

	
	//make space for the cells

	m_SOMCells = vector<LatticeCell*>(Width*Height);

	try
	{
		//put cells into the lattice

		for (i=0; i<Width; i++)
		{
			for (j=0; j<Height; j++)
			{
				currentCell = new LatticeCell;
				currentCell->XPos=i;
				currentCell->YPos=j;
				currentCell->Cell = new SOMCellType(NumberOfWeights);
				m_SOMCells.push_back(currentCell);
			}
		}
	}
	catch(...)
	{
		bSuccess=false;
	}

	return bSuccess;
}

template<class SOMCellType>bool CSOMLattice<SOMCellType>::Train(DataSet *data, long numIterations)
{
	/*	Robert Kirk DeLisle
		24 March 2004
		
		Purpose:	Trains the SOM for the desired number of iterations.
					One iteration is defined as comparison of one observation
					and adjustment of weights.  <this may be modified to an epoch.>

		Parameters:	data	-	pointer to the DataSet object from where to derive the observations

		Return:		true if successful
					false otherwise
	*/

	return true;
}

template<class SOMCellType> CSOMLattice<SOMCellType>::~CSOMLattice()
{
	/*	Robert Kirk DeLisle
		25 March 2004

		Purpose:	Release references as needed.
	
		Parameters:	none

		Return:		none
	*/

	int i;	//loop index


	for (i=0; i<m_SOMCells.size(); i++)
	{
		if (m_SOMCells[i] != 0)
		{
			delete m_SOMCells[i];
		}
	}

	m_SOMCells.clear();

}
 
[edited by - KirkD on March 25, 2004 10:56:20 AM] [edited by - KirkD on March 25, 2004 1:43:28 PM]
Advertisement
I believe if you put the implementation of the functions in the header file instead of in the source file it should work..
Nope. Here are the errors:

SOM.obj : error LNK2001: unresolved external symbol "public: __thiscall CSOMLattice::~CSOMLattice(void)" (??1?$CSOMLattice@VCSOMCellFloat@@@@QAE@XZ)

SOM.obj : error LNK2001: unresolved external symbol "public: bool __thiscall CSOMLattice::Train(class DataSet *,long)" (?Train@?$CSOMLattice@VCSOMCellFloat@@@@QAE_NPAVDataSet@@J@Z)

SOM.obj : error LNK2001: unresolved external symbol "public: bool __thiscall CSOMLattice::Initialize(long,long,long)" (?Initialize@?$CSOMLattice@VCSOMCellFloat@@@@QAE_NJJJ@Z)
OK, can you modify your original post and put the code inside some source code tags [ source ] and [ / source ] without the spaces, it''ll stop the board getting rid of your template parameters for one, and do syntax highlighting for another.

Apart from that, it looks like you do have the classic implementation-must-be-in-header problem but since devlaxe suggested that already and you said it didn''t work perhaps something else is improper. I''ll take another look when I get home (and hopefully you''ve updated your post with source tags).

-Mezz
Modified. Thank you for your help.

-Kirk

EDIT:: My mistake! I tried it again and got it to compile with the implementation in the header file. Is this specifically a problem with the VC++ compiler?

Now I have to address all the other problems with this code. Thank you both!


[edited by - KirkD on March 25, 2004 3:00:22 PM]
Separate compilation of template function definitions (edit: without explicit instantiation) is allowed by the C++ standard only in the presence of the export keyword. Unfortunatly, no version of MSVC supports export. For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.

[edited by - SiCrane on March 25, 2004 3:15:00 PM]

This topic is closed to new replies.

Advertisement