Template trouble!!!

  • Thread starter Salvatore Di Fazio
  • Start date
S

Salvatore Di Fazio

Hi guys,
I made the following template:

template <class RECORD, int iSize>
class CArrayTable
{
public:
CArrayTable();
~CArrayTable();

// ...
};

template <class RECORD, int ISIZE>
CArrayTable<RECORD, iSize>::CArrayTable()
{
m_iCount = ISIZE;
m_iIndex = 0;
m_pRECORD = new RECORD[iElement];
}

when I try to use this template:


CArrayTable<CIntAirport, 10> Table;

I receive a linker error:

SDPF error LNK2019: unresolved external symbol "public: __thiscall
CArrayTable<class CIntAirport,10>::~CArrayTable<class
CIntAirport,10>(void)" (??1?$CArrayTable@VCIntAirport@@$09@@QAE@XZ)
referenced in function "public: int __thiscall
CMainFrame::ImportDafifFile(class CStringArray &,class CStringArray
&,class CStringArray &)"
(?ImportDafifFile@CMainFrame@@QAEHAAVCStringArray@@00@Z)


Anybody know why?
 
T

TB

Salvatore Di Fazio skrev:
Hi guys,
I made the following template:

template <class RECORD, int iSize>
class CArrayTable
{
public:
CArrayTable();
~CArrayTable();

// ...
};

template <class RECORD, int ISIZE>
CArrayTable<RECORD, iSize>::CArrayTable()
{
m_iCount = ISIZE;
m_iIndex = 0;
m_pRECORD = new RECORD[iElement];
}

when I try to use this template:


CArrayTable<CIntAirport, 10> Table;

I receive a linker error:

SDPF error LNK2019: unresolved external symbol "public: __thiscall
CArrayTable<class CIntAirport,10>::~CArrayTable<class
^^^^^^^^^^^^ destructor undefined
 
I

Ivan Vecerina

: Hi guys,
: I made the following template:
:
: template <class RECORD, int iSize>
: class CArrayTable
: {
: public:
: CArrayTable();
: ~CArrayTable();
:
: // ...
: };
:
: template <class RECORD, int ISIZE>
iSize I assume ??
: CArrayTable<RECORD, iSize>::CArrayTable()
: {
: m_iCount = ISIZE;
: m_iIndex = 0;
: m_pRECORD = new RECORD[iElement];
iSize I assume??
: }
Is the destructor ~CArrayTable defined anywhere?

Note that unless you expect iSize to be large,
you could define the array of elements as a data
member if its size is determined as a template parameter:
RECORD m_records[iSize];

: when I try to use this template:
:
:
: CArrayTable<CIntAirport, 10> Table;
:
: I receive a linker error:
:
: SDPF error LNK2019: unresolved external symbol "public: __thiscall
: CArrayTable<class CIntAirport,10>::~CArrayTable<class
: CIntAirport,10>(void)" (??1?$CArrayTable@VCIntAirport@@$09@@QAE@XZ)
: referenced in function "public: int __thiscall
: CMainFrame::ImportDafifFile(class CStringArray &,class CStringArray
: &,class CStringArray &)"
: (?ImportDafifFile@CMainFrame@@QAEHAAVCStringArray@@00@Z)
:
:
: Anybody know why?
The message indicates that the destructor ~CArrayTable is
not defined.
Is it?


Ivan
 
E

Earl Purple

Salvatore said:
Hi guys,
I made the following template:

template <class RECORD, int iSize>
class CArrayTable
{
public:
CArrayTable();
~CArrayTable();

// ...
};

template <class RECORD, int ISIZE>
CArrayTable<RECORD, iSize>::CArrayTable()
{
m_iCount = ISIZE;
m_iIndex = 0;
m_pRECORD = new RECORD[iElement];
}

First a couple of style issues (which you can choose to ignore).

1. Don't begin your class names with a prefix C. They are not MFC
classes.
2. m_ prefix for class members is not so bad but otherwise avoid the
Hungarian notation. So m_index is better than m_iIndex.

Now some issues you can't ignore:

3. C++ is case-sensitive so ISIZE as the template parameter must remain
all uppercase one line below.

4. iElement is undefined.
5. If you allocate an array with new in the constructor then the
destructor must be defined (it is declared here but not defined) and it
must call delete[]. Plus you must either handle assignment and
copy-construction or disable them by declaring them as private. If you
use vector you can avoid.

6. What is ISIZE used for anyway? Is it meant to be a fixed-size array?
If so why are you using new at all?

7. What is m_iCount used for? If the size of the array remains constant
throughout, you can use ISIZE throughout your class whenever you need
to know it.

8. What is iIndex used for? A collection should generally not hold its
own position. Instead you have iterators pointing to locations in a
collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top