Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a struct in a template class, undefined for member functions

I'm currently trying to implement a sort algorithm in a list template class using node structures private to the list class. I'm using a few private recursive functions which return a pointer to a node type which causes g++ to give me a declaration error. Here's a sample of what I have -

template<class T>
class SList
{
private:
    struct NODE
    {
        T* elem;
        NODE* next;
    } *_head, *_tail;

NODE* sort(NODE* node);

public:
    //other declarations...

}

template<class T>
NODE* SList<T>::sort(NODE* node) //Error: 'NODE' does not name a type
{
    //sorting algorithm
}

Is this a limitation of c++ or am I missing something?

like image 264
Lindenk Avatar asked Nov 26 '12 06:11

Lindenk


People also ask

Can a struct have a member function?

A struct can indeed have public and private sections, member functions as well as member variables, and special functions such as constructors, destructors and operators.

Can member functions be declared as template?

Member functions can be function templates in several contexts. All functions of class templates are generic but aren't referred to as member templates or member function templates. If these member functions take their own template arguments, they're considered to be member function templates.

Can a struct be a template?

The entities of variable, function, struct, and class can have templates.

Can member functions be declared as template in C++?

Template declarations (class, function, and variables (since C++14)) can appear inside a member specification of any class, struct, or union that aren't local classes. Partial specializations of member template may appear both at class scope and at enclosing namespace scope.


3 Answers

Since Node is an internal class you need to tell the compiler where Node's definition comes from.

Furthermore, Node's definition changes depending on what SList's template parameter is (it is a dependent type)

So you have to explicitly refer to Node as such:

template<class T>
typename SList<T>::NODE* SList<T>::sort(typename SList<T>::NODE* node) 
{
    //sorting algorithm
}
  • Note the typename because Node is a dependent type (it depends on the type of SList)
  • Note the SList<T>::Node because Node is a type dependent on SList's type.
like image 133
Steve Lorimer Avatar answered Oct 22 '22 07:10

Steve Lorimer


Below works fine:

template<class T>
typename SList<T>::NODE* SList<T>::sort(typename SList<T>::NODE* node)
^^^^^^^^^^^^^^^^^^^                     ^^^^^^^^^^^^^^^^^^^

Is this a limitation of c++

No. Because there can be any structure/type named NODE outside the scope of SList<>; so actually it's a facility which C++ provides you where you can have same name types in different scope.

"Why I need keyword typename" can be found here.

like image 45
iammilind Avatar answered Oct 22 '22 08:10

iammilind


The type you want to refer to is within SList, thus you must refer to it as such:

template<class T>
typename SList<T>::NODE* SList<T>::sort(typename SList<T>::NODE* node);
like image 1
Agentlien Avatar answered Oct 22 '22 07:10

Agentlien