开发者

Returning a pointer to an object ( C++ Templates ) Error

开发者 https://www.devze.com 2023-04-08 07:34 出处:网络
I\'ve been asked to switch three classes for a university assignment over to templates to make them more generic but I\'m having problems when trying to return a pointer to an object from a template f

I've been asked to switch three classes for a university assignment over to templates to make them more generic but I'm having problems when trying to return a pointer to an object from a template function.

Compiler: GCC Version - 3.4.4 ( Cygwin )

Here is the (.h):

  namespace node_class
    {
    template <class Item>
    class node
    {
        public:

            // CONSTRUCTOR      
            node(const Item& initial_data = 0, node* initial_link = NULL);
            ~node(){}

            // MUTATING MEMBER FUNCTIONS
            void set_data(const Item& new_data);
            void set_link(node* new_link);

            // NON-MUTATING MEMBER FUNCTIONS
            const node* link(void) const;
            node* link(void);

            const Item& data(void) const;
            Item& data(void);

        private:

            Item data_field;
            node* next_field;
    };
    }

Here are the two function implementations for returning a pointer to 开发者_如何学编程an object:

 template<class Item>
    const node<Item>::node* node<Item>::link(void) const
    {
        return(next_field);
    }

    template<class Item>
    node<Item>::node* node<Item>::link(void) 
    {
        return(next_field);
    }

Here is the errors I keeps throwing when trying to compile:

$ g++ -Wall node.h

node.h:73: error: expected init-declarator before '*' token
node.h:73: error: expected ';' before '*' token
node.h:79: error: expected constructor, destructor, or type conversion before '*' token
node.h:79: error: expected ';' before '*' token

At the moment, I have a separate (.h) and (.template) with the .template being included by a include directive in the header file; But, I'm still getting these same error messages even when the implementation of the template functions is included in the (.h) like it's supposed to be.

Any help would be great, thank you.


node<Item>::node is not a type. Make your return type node<Item>*.

The method declarations should be:

const node<Item>* link(void) const;
node<Item>* link(void);

The definition of these methods should be:

template<class Item>
const node<Item>* node<Item>::link(void) const
{
    return(next_field);
}

template<class Item>
node<Item>* node<Item>::link(void) 
{
    return(next_field);
}


The node<Item>::node* should just be a node<Item>*:

template<class Item>
const node<Item>* node<Item>::link(void) const
{
    return(next_field);
}

template<class Item>
node<Item>* node<Item>::link(void) 
{
    return(next_field);
}

Edit

You seem a little confused on how templates work, so I'll give an overview:

The first thing to understand is: there is no class name node. None at all. There is a class template named node. What does this mean? For each type Item which you apply to this template, there is a corresponding class named node<Item>. To repeat: if Item is a type, then node<Item> is a class, but node is never a class.

But you're probably wondering "If node isn't a type, why can I use it like a type?", as in

template<class Item>
class node
{
        node* next_field;
};

But here, node is just shorthand for node<Item>. Don't get confused just because it looks like node is a type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号