Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

generic class problem 1

Status
Not open for further replies.

jianhuay

Programmer
Apr 11, 2001
13
AU
Hi all,
I inserted a Sorter class (quick sort algorithm helper class) into my MFC
project as a generic class (other than MFC class), then I got two files
Sort.h and Sort.cpp; The project can be compiled, but it has problem
with linking --- the error message is something looks like "unresolved
external symbol ......".

However when I merged Sort.h and Sort.cpp into only one file Sort.h,
and put declare and implementation together, my project got compiled
and linked successfully. So I wonder what's wrong with separating
declare and implementation of a generic class. Do I need to configure
the Project Settings for project compiling and linking or something else.

Thanks.

Jian Hua
 
Hi

No, you normally separate the header from the implementation.

And no, you don't need something specific at the compiler or linker level. All's automatic.

To answer this question, one needs to look at the code ...

HTH

Thierry
 
Hi Thierry,

Thank you for your comments.

I listed the code here for your checking (if it does not trouble you a lot).

Thanks again.

Jian Hua

=====================================

My Sorter.h is as follows:

// Sorter.h
#if !defined(TSORTER_INCLUDED_)
#define TSORTER_INCLUDED_

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

template <class T>
class Sorter
{
protected:
unsigned int n;
static void Swap (T& x, T& y);

public:
virtual void DoSort (T *pT);
};


template <class T>
class ExchangeSorter : public Sorter<T>
{

};

template <class T>
class QuickSorter : public ExchangeSorter<T>
{
protected:
static unsigned int const cutOff;
virtual unsigned int SelectPivot (T * pT, unsigned int left, unsigned int right);

public:
void DoSort (T * pT, unsigned int left, unsigned int right);
};

#endif

=========================================

And my Sorter.cpp looks like this:

//Sorter.cpp
#include &quot;stdafx.h&quot;
#include &quot;AccessLogParser.h&quot;
#include &quot;Sorter.h&quot;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

template <class T>
void Sorter<T>::Swap(T& x, T& y)
{
T const tmp = x;
x = y;
y = tmp;
}

template <class T>
void Sorter<T>::DoSort(T *pT)
{
//code here
}

template <class T>
unsigned int QuickSorter<T>::SelectPivot(T * pT, unsigned int left, unsigned int right)
{
//code here
}

template <class T>
void QuickSorter<T>::DoSort(T * pT, unsigned int left, unsigned int right)
{
//code here
}
 
The problem you are most likely having is that you did not add the files to your project.

if this is the error,

&quot;fatal error LNK1120: 1 unresolved externals&quot;

go to the project menu -> add to project -> files

and add the .h and .cpp file to the project. Then you will not need to put the .h & the .cpp file in one file.

Matt

 
I did add Sorter.cpp and Sorter.h in my project. Some other comments?

Thanks.

Jian Hua
 
Hi!

Compilator can't find implementation of yours methods.

When you use templates, you should write implementation in the same file.

For example, you can see source code from STL, f.i. <vector>.

========================
template <class T>
class Sorter
{
protected:
unsigned int n;
static void Swap (T& x, T& y)
{
T const tmp = x;
x = y;
y = tmp;
}


public:
virtual void DoSort (T *pT);
};
========================
or
========================
template <class T>
class Sorter
{
protected:
unsigned int n;
static void Swap (T& x, T& y);

public:
virtual void DoSort (T *pT);
};

// in the same file...

template <class T>
void Sorter<T>::Swap(T& x, T& y)
{
T const tmp = x;
x = y;
y = tmp;
}
========================

With best regards,
Zaki
zaki-maksyutov@yandex.ru

 
Hi ZakiMaksyutov,

Thanks a lot. With your comments, I don't need to spend time to try this problem any more.

cheers,

Jian Hua
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top