Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a pointer to an object ( C++ Templates ) Error
    primarykey
    data
    text
    <p>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.</p> <p>Compiler: GCC Version - 3.4.4 ( Cygwin )</p> <p>Here is the (.h):</p> <pre><code> namespace node_class { template &lt;class Item&gt; class node { public: // CONSTRUCTOR node(const Item&amp; initial_data = 0, node* initial_link = NULL); ~node(){} // MUTATING MEMBER FUNCTIONS void set_data(const Item&amp; new_data); void set_link(node* new_link); // NON-MUTATING MEMBER FUNCTIONS const node* link(void) const; node* link(void); const Item&amp; data(void) const; Item&amp; data(void); private: Item data_field; node* next_field; }; } </code></pre> <p>Here are the two function implementations for returning a pointer to an object:</p> <pre><code> template&lt;class Item&gt; const node&lt;Item&gt;::node* node&lt;Item&gt;::link(void) const { return(next_field); } template&lt;class Item&gt; node&lt;Item&gt;::node* node&lt;Item&gt;::link(void) { return(next_field); } </code></pre> <p>Here is the errors I keeps throwing when trying to compile:</p> <pre><code>$ 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 </code></pre> <p>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.</p> <p>Any help would be great, thank you. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload