Re: c2244 and namespace error.
On Oct 10, 2:15 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
<cablep...@gmail.com> wrote in message
news:1192046830.793792.190220@o80g2000hse.googlegroups.com...
On Oct 10, 12:36 pm, Norbert Unterberg <nunterb...@newsgroups.nospam>
wrote:
cablep...@gmail.com schrieb:
Recently i did some compilation test that resulted in C2244 unable to
match function declaration and function definition.
example..
inside the hpp file.
namespace something {
template <typename T,
template <typename ELEM, typename = std::allocator<ELEM> >
class CONT
extern void select_sort(CONT<T>&, typename CONT<T>::iterator, const
typename CONT<T>::size_type&, const typename CONT<T>::size_type&);
}
inside the c file
template <typename T,
template <typename ELEM, typename = std::allocator<ELEM> >
class CONT
void Something::select_sort(CONT<T>& array, typename CONT<T>::iterator
^^^^^^^^^
something instead of Something?
Norbert
yeah my bad.. i tried that with anoynomus namespace too..
hmm and without extern keyword
anonymous namespace won't work: it is specific to each compilation unit and
is the opposite of extern. C uses the keyword "static" for this, anon
namespace is the C++ way.
I still get c2244 error.
k i did some more testing and track the pattern of my compile error.
I notice that you can't have "void" return in namespace template
declaration and definition.
example
SortingAlgo.hpp
namespace algocpw
{
/*********************************************
* sifts the array, makes changes to it *
* @param arr the array *
* @param start the index to start at *
* @param count the number of time to sift *
*********************************************/
template <
typename T,
template <typename ELEM, typename = std::allocator<ELEM> >
class CONT
void sift(CONT<T>*, typename CONT<T>::iterator, const int&);
}
SortingAlgodef.hpp
template <
typename T,
template <typename ELEM, typename = std::allocator<ELEM> >
class CONT
void algocpw::sift(CONT<T>* arr, typename CONT<T>::iterator start,
const int& count)
{
typedef typename CONT<T>::iterator iter;
iter root(start);
iter child;
while ((std::distance(arr->begin(), root)*2+1) < count)
{
child = boost::next(arr->begin(), std::distance(arr->begin(),
root) * 2 + 1);
if (std::distance(arr->begin(), child) < count-1 &&
*child < *boost::next(child, 1))
++child;
if (*root < *child)
{
std::iter_swap(root, child);
root = child;
}
else
return;
}
}
visual studio gives this error
../cs512/c++/cppdef/sortingAlgo.cpp(123) : error C2244:
'algocpw::sift' : unable to match function definition to an existing
declaration
c:\cpw\msvc\cs512\c++\cppdef\../sortingAlgo.hpp(81) : see
declaration of 'algocpw::sift'
A fix to this problem would be
namespace algocpw
{
// put the definition in here.
template <
typename T,
template <typename ELEM, typename = std::allocator<ELEM> >
class CONT
void sift(CONT<T>* arr, typename CONT<T>::iterator start, const int&
count)
{
typedef typename CONT<T>::iterator iter;
iter root(start);
iter child;
while ((std::distance(arr->begin(), root)*2+1) < count)
{
child = boost::next(arr->begin(), std::distance(arr->begin(),
root) * 2 + 1);
if (std::distance(arr->begin(), child) < count-1 &&
*child < *boost::next(child, 1))
++child;
if (*root < *child)
{
std::iter_swap(root, child);
root = child;
}
else
return;
}
}
}