• Advertisement
Sign in to follow this  

Name Conflict?

This topic is 2163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am trying to write a template of array for practicing. And I define 2 GetArray functions in the class

const T* TArray::GetArray () const;
T* TArray::GetArray ();

and implement it seperately(all of them just return the array)

template <class T>
T* TArray<T>::GetArray ()
{
return m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
const T* TArray<T>::GetArray () const
{
return m_atArray;
}

My program throw me 2 errors and say can not match the defination and declaration


1>Compiling...
1>LumPyMain.cpp
1>c:\users\jerry\desktop\lumpy\lumpy\tarray.inl(64) : error C2244: 'LumPy::TArray<T>::GetArray' : unable to match function definition to an existing declaration
1> c:\users\jerry\desktop\lumpy\lumpy\tarray.h(26) : see declaration of 'LumPy::TArray<T>::GetArray'
1> definition
1> 'T *LumPy::TArray<T>::GetArray(void)'
1> existing declarations
1> 'const T *LumPy::TArray<T>::GetArray(void) const'
1>Generating Code...
1>Compiling...
1>MyApplication.cpp
1>c:\users\jerry\desktop\lumpy\lumpy\tarray.inl(64) : error C2244: 'LumPy::TArray<T>::GetArray' : unable to match function definition to an existing declaration
1> c:\users\jerry\desktop\lumpy\lumpy\tarray.h(26) : see declaration of 'LumPy::TArray<T>::GetArray'
1> definition
1> 'T *LumPy::TArray<T>::GetArray(void)'
1> existing declarations
1> 'const T *LumPy::TArray<T>::GetArray(void) const'
1>Generating Code...

Any one knows what is going on? Thx advanced

Share this post


Link to post
Share on other sites
Advertisement
The following compiles for me (on GCC)
template<typename T>
class TArray
{
public:
const T* GetArray () const;
T* GetArray ();
private:
T *m_atArray;
};

template <class T>
T* TArray<T>::GetArray ()
{
return m_atArray;
}


template <class T>
const T* TArray<T>::GetArray () const
{
return m_atArray;
}

int main()
{
TArray<int> a;
a.GetArray();
const TArray<int> &c = a;
c.GetArray();
}

Can you produce a minimal example that demonstrates this error?

Share this post


Link to post
Share on other sites
I might be able to offer a guess if you could pair the file names and line numbers with the posted code. And if those line numbers are not in the code posted, please post it.

Share this post


Link to post
Share on other sites
Thanks for replying firstly
1>c:\users\jerry\desktop\lumpy\lumpy\tarray.inl(64) : error C2244: 'LumPy::TArray<T>::GetArray' : unable to match function definition to an existing declaration
1> c:\users\jerry\desktop\lumpy\lumpy\tarray.h(26) : see declaration of 'LumPy::TArray<T>::GetArray'
the 64th line in tarray.inl is

template <class T> //60
T* TArray<T>::GetArray ()//61
{//62
return m_atArray;//63
} //64
and the 26th line in tarray.h is
const T* TArray::GetArray () const;//26
but after it I have declared
T* TArray::GetArray ();
which should be paired with T* TArray<T>::GetArray ()//61

If I comment out one of them the program could compile. But what is wrong here?
also I have

template <class T>
const T* TArray<T>::GetArray () const
{
return m_atArray;
}
as well to pair with
const T* TArray::GetArray () const;

Share this post


Link to post
Share on other sites
Post all the code necessary to reproduce your error independently. Someone should be able to copy and paste from your post into their own compiler and see the same error you're getting. But try not to post code not relevant to the problem. If you look at rip-off's post that's the kind of thing you should be aiming for: enough to see what the problem is but without irrelevant details. If in doubt copy your project and start deleting things until all that's left is only the code that produces your error.

Share this post


Link to post
Share on other sites
//TArray.h
#ifndef LUMPYTARRAY_H
#define LUMPYTARRAY_H

namespace LumPy{

template <class T>
class TArray
{
public:
// construction and destruction
TArray (int iQuantity, int iGrowBy);
TArray (const TArray& rkObject);
~TArray ();

// assignment
TArray& operator= (const TArray& rkObject);

// element access, index i must be in range
int TArray::GetQuantity () const;
const T* TArray::GetArray () const;
T* TArray::GetArray ();
T& TArray::operator[] (int i);
const T& operator[] (int i) const;

// add new element, array will dynamically grow if necessary
void Append (const T& rtElement);
void SetElement (int i, const T& rtElement);

// Remove the element at the specified index. The elements occurring
// after that one are shifted so that the array remains contiguous. After
// the shift, but before the decrement on quantity, array[quantity-1] is
// a duplicate of array[quantity-2]. The element at index quantity-1 is
// reconstructed using the default constructor for class T. Then the
// quantity is decremented.
void Remove (int i);

// all elements are set to the default object of class T
void RemoveAll ();
// dynamic growth, new array elements are default constructed

void SetMaxQuantity (int iNewMaxQuantity, bool bCopy);
int GetMaxQuantity () const;
void SetGrowBy (int iGrowBy);
int GetGrowBy () const;

private:
int m_iQuantity, m_iMaxQuantity, m_iGrowBy;
T* m_atArray;
};
#include "TArray.inl"
}

#endif
//TArray.inl

//----------------------------------------------------------------------------
template <class T>
TArray<T>::TArray (int iQuantity, int iGrowBy)
{
assert( iQuantity >= 0 && iGrowBy >= 0 );

if ( iQuantity < 0 )
iQuantity = 0;

if ( iGrowBy < 0 )
iGrowBy = 0;

m_iQuantity = 0;
m_iMaxQuantity = iQuantity;
m_iGrowBy = iGrowBy;
m_atArray = ( m_iMaxQuantity > 0 ? new T[m_iMaxQuantity] : 0 );
}
//----------------------------------------------------------------------------
template <class T>
TArray<T>::TArray (const TArray& rkObject)
{
m_atArray = 0;
*this = rkObject;
}
//----------------------------------------------------------------------------
template <class T>
TArray<T>::~TArray ()
{
delete[] m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
TArray<T>& TArray<T>::operator= (const TArray& rkObject)
{
m_iQuantity = rkObject.m_iQuantity;
m_iMaxQuantity = rkObject.m_iMaxQuantity;
m_iGrowBy = rkObject.m_iGrowBy;

delete[] m_atArray;
if ( m_iMaxQuantity > 0 )
{
m_atArray = new T[m_iMaxQuantity];
for (int i = 0; i < m_iMaxQuantity; i++)
m_atArray = rkObject.m_atArray;
}
else
{
m_atArray = 0;
}

return *this;
}
//----------------------------------------------------------------------------
template <class T>
int TArray<T>::GetQuantity () const
{
return m_iQuantity;
}
//----------------------------------------------------------------------------
template <class T>
T* TArray<T>::GetArray ()
{
return m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
const T* TArray<T>::GetArray () const
{
return m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
T& TArray<T>::operator[] (int i)
{
assert( 0 <= i && i < m_iQuantity );
if ( i < 0 )
i = 0;
if ( i >= m_iQuantity )
i = m_iQuantity-1;

return m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
const T& TArray<T>::operator[] (int i) const
{
assert( 0 <= i && i < m_iQuantity );
if ( i < 0 )
i = 0;
if ( i >= m_iQuantity )
i = m_iQuantity-1;

return m_atArray;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::Append (const T& rtElement)
{
if ( ++m_iQuantity > m_iMaxQuantity )
{
assert( m_iGrowBy > 0 );
if ( m_iGrowBy > 0 )
{
// increase the size of the array
SetMaxQuantity(m_iMaxQuantity+m_iGrowBy,true);
}
else
{
// cannot grow the array, overwrite the last element
--m_iQuantity;
}
}

m_atArray[m_iQuantity-1] = rtElement;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::SetElement (int i, const T& rtElement)
{
assert( i >= 0 );
if ( i < 0 )
i = 0;

if ( i >= m_iQuantity )
{
if ( i >= m_iMaxQuantity )
{
assert( m_iGrowBy > 0 );
if ( m_iGrowBy > 0 )
{
// increase the size of the array
if ( i+1 >= m_iMaxQuantity )
{
int iN =
(int)(0.5f+(i+1-m_iMaxQuantity)/(float)m_iGrowBy);
iN++;
SetMaxQuantity(m_iMaxQuantity+iN*m_iGrowBy,true);
}
}
else
{
// cannot grow the array, overwrite the last element
i = m_iQuantity-1;
}
}
m_iQuantity = i+1;
}

m_atArray = rtElement;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::Remove (int i)
{
assert( 0 <= i && i < m_iQuantity );
if ( i < 0 || i >= m_iQuantity )
return;

for (int j = i+1; j < m_iQuantity; i = j++)
m_atArray = m_atArray[j];
m_atArray[m_iQuantity-1] = T();
m_iQuantity--;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::RemoveAll ()
{
for (int i = 0; i < m_iQuantity; i++)
m_atArray = T();
m_iQuantity = 0;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::SetMaxQuantity (int iNewMaxQuantity, bool bCopy)
{
assert( iNewMaxQuantity >= 0 );
if ( iNewMaxQuantity < 0 )
iNewMaxQuantity = 0;

if ( iNewMaxQuantity == 0 )
{
delete[] m_atArray;
m_iQuantity = 0;
m_iMaxQuantity = 0;
return;
}

if ( iNewMaxQuantity != m_iMaxQuantity )
{
T* atNewArray = new T[iNewMaxQuantity];

if ( bCopy )
{
int iCopyQuantity;
if ( iNewMaxQuantity > m_iMaxQuantity )
iCopyQuantity = m_iMaxQuantity;
else
iCopyQuantity = iNewMaxQuantity;

for (int i = 0; i < iCopyQuantity; i++)
atNewArray = m_atArray;

if ( m_iQuantity > iNewMaxQuantity )
m_iQuantity = iNewMaxQuantity;
}
else
{
m_iQuantity = 0;
}

delete[] m_atArray;
m_atArray = atNewArray;
m_iMaxQuantity = iNewMaxQuantity;
}
}
//----------------------------------------------------------------------------
template <class T>
int TArray<T>::GetMaxQuantity () const
{
return m_iMaxQuantity;
}
//----------------------------------------------------------------------------
template <class T>
void TArray<T>::SetGrowBy (int iGrowBy)
{
assert( iGrowBy >= 0 );
if ( iGrowBy >= 0 )
m_iGrowBy = iGrowBy;
}
//----------------------------------------------------------------------------
template <class T>
int TArray<T>::GetGrowBy () const
{
return m_iGrowBy;
}
//----------------------------------------------------------------------------
//in other class

class XX{
typedef TArray<Initializer> InitializerArray;
}

Share this post


Link to post
Share on other sites
Inside the class definition you don't prefix the member declarations with TArray:: . Just use:


const T* GetArray () const;
T* GetArray ();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement