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 SkipVought 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 3

Status
Not open for further replies.

jianhuay

Programmer
Apr 11, 2001
13
0
0
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

 
You use a function implemented in a .lib file what is missed in your project settings. John Fill
ivfmd@mail.md
 
The error sounds familiar. Did you add the Sort.cpp to the project?

Think of the #include statement as adding the entire file in place of the &quot;#include <file>&quot;. If you compile to the preprossessor you will actually see something along these lines. By moving the functionality into the headder file, in essence you have included the cpp into the program indirectly. Problems could arrise however with multiple declarations of the functions if you are not careful.

I would double check the file view and see if sort.cpp is there or not.

Matt
 
Either if your Sorter class was not created using the MFC Wizard, or if your Sorter class is not an MFC class, you will get that message. I used to face this problem myself. So, just add this statement as the first line in the Sort.cpp file:

#include &quot;stdafx.h&quot;

It should work after this.

Andyrau
 
Hello everyone,

I'm back from my holiday and I thank you for your reply.

I read the replies from JohnFill, Zyrenthian and Andyrau, and ensured Sorter.cpp was added to my project, also I had #include &quot;stdafx.h&quot; in Sorter.cpp. However it still doesn't work. Maybe I didn't get the point, I'll check it again.

Thanks.

Jian Hua
 
I did add them (Sorter.h and Sorter.cpp) in my project. Other comments?

Thanks.

Jian Hua
 
Thanks for everyone's comments.

This morning I got a reply from a nice guy (Microsoft: Visual C++ forum) saying that
when using templates, one should write implementation in the same file, otherwise compilator can't find implementation of methods.

I did use template classes, and my code is as follows:

cheers,
Jian Hua
=========================================

/***** 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
/********************************Sorter.h******/

/******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
}
/********************************Sorter.cpp******/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top