المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Template Class [modified]



C++ Programming
05-03-2009, 01:11 PM
I am using templates for a class for the first time and I cannot figure out what I am doing wrong. If I use the first constructor the code compiles fine, if I use the second which takes 2 arguments I get a compiler error.

Any help would be greatly appreciated.

This code works

// Test AssocArray
AssocArray myArray();


This code will generate LNK2019 Unresolved External (http://msdn.microsoft.com/en-us/library/799kze2z(VS.71).aspx)

// Test AssocArray
AssocArray myArray(2, 2);


Class Definition

template
class AssocArray
{
public:
AssocArray();
AssocArray(int size, int resizeBy);

int GetCount() { return m_nItems; }

void SetNewSize(int size, bool copy = true);
void IncreaseSize(int size, bool copy = true);

TValue operator [](int i);
TValue operator [](string s);

private:
int m_nItems;
int m_nMaxItems;
int m_nResizeBy;

KeyValuePair* m_array;
};


Class Constructors

template
AssocArray::AssocArray()
{
m_nItems = 0;
m_nMaxItems = 2;
m_nResizeBy = 2;
m_array = new KeyValuePair[2];
}

template
AssocArray::AssocArray(int size, int resizeBy)
{
m_nItems = 0;
m_nMaxItems = size;
m_nResizeBy = resizeBy;
m_array = new KeyValuePair[size];
}


modified on Sunday, May 3, 2009 5:04 AM