Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:28:04+00:00 2026-05-28T00:28:04+00:00

I have made a template class for a node in a linked list and

  • 0

I have made a template class for a node in a linked list and I’m trying to have it output its contents to an output stream by overloading <<. However my current code:

#include <iostream>
using namespace std;
template<class NType> class Node;

template<class NType>
class Node {
private:
        void deletePointer(NType* p);
public:
        NType data;
        Node *prev, *next;

        template<typename T>
        struct is_pointer { static const bool value = false; };

        template<typename T>
        struct is_pointer<T*> { static const bool value = true; };

        Node();
        Node(NType data);
        ~Node();
};  

int main() {
        Node<int> *n1 = new Node<int>();
        Node<int> *n2 = new Node<int>(10);

        std::cout << "Node 1: " << n1 << std::endl;
        std::cout << "Node 2: " << n2 << std::endl;
}

template<class NType> inline std::ostream & operator << (std::ostream& out, const Node<NType> &node){
        out << node.data;
        return out;
}

template<class NType> inline Node<NType>::Node()
            :data(NULL), prev(NULL), next(NULL)
{
}

template<class NType> inline Node<NType>::Node(NType data)
            :data(data), prev(NULL), next(NULL)
{
}

template<class NType> inline Node<NType>::~Node(){
        if(is_pointer<NType>::value){
                deletePointer(&data);
        } else {
                return;
        }
}

template<class NType> inline void Node<NType>::deletePointer(NType* p){
    delete p;
}

Outputs memory locations rather than the data within the nodes. This happens with primitive types such as int and the like as if it didn’t know what kind of data was in the NType container.

Node 1: 0x741010
Node 2: 0x741030
Node 3: 0x741070
Node 4: 0x741090

I’ve tried using typename rather than class but still no dice… Is there any way to dynamically find out what type the template is using and cast or something prior to insertion? I know I can make a ton of redundant code for all the primitives but that seems wasteful and unnecessary.

If it helps any, I’m compiling on Arch Linux x64 with GCC v4.6.2 20111223

Edit: Since a lot of people are mentioning it. I’ve also tried putting the class outside as a friend and as a stand alone function neither of which work because the stream outputs address rather than the data itself regardless of where I put it. There are no private data values to be accessed so it’s OK for it to not be a friend.

Edit:
Test case: http://ideone.com/a99u5
Also updated source above.

Edit:
Added the remaining portion of my code to assist Aaron in his understanding of the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:28:04+00:00Added an answer on May 28, 2026 at 12:28 am

    Your code declares the operator<< as a member function, so it would actually take the this pointer as first argument and ostream as second. Instead it needs to be a free function:

    template<class NType> class Node {
    public:
        NType data;
        Node *prev, *next;
    };
    //Note how this is declared outside of the class body, so it is a free function instead of a memberfunction
    template<class NType> inline std::ostream& operator<<(std::ostream& out, const Node<NType>& val){
        out << val.data;
        return out;
    }
    

    however if your operator<< needs access to private data you need to declare it as a friend function instead:

    template<class NType> class Node {
    public:
        NType data;
        Node *prev, *next;
        friend std::ostream& operator<<(std::ostream& out, const Node& val){
            out << val.data;
            return out;
        }
    };
    

    Now for your output: If your operator<< was invoked the compiler would know the type of NType and do the right thing when streaming the data member. However since your operator<< should not have worked (as written) and it seems to give you memoryaddresses as output I would assume you have something like the following:

    Node* n = new Node();
    std::cout<<n;
    //When it should be:
    std::cout<<*n;
    

    Now just for curiosity sake: Why are you implementing what looks curiously like a linked list yourself instead of simply using std::list?

    Edit:
    Now that we can see the testcase it seems the assumptions about how the operator<< was called was correct. The output needs to be changed to:

    std::cout << "Node 1: " << *n1 << std::endl;
    std::cout << "Node 2: " << *n2 << std::endl;
    

    to actually invoke the operator<< for Node, instead of the generic one for T*

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a self made data structure (for example linked list) that works well,
I have a template class that I have made called hash . My template
I have a template I made that sets variables that rarely change, call my
I have made a class which a form can inherit from and it handles
I have a Binary-Tree class template with several public and private member functions. When
I made my own class template to maintain pair of elements of any type
I have made a List of Lists implementation of a sparse matrix, and I've
I have a recursive template definition (I just made up that term). I think
I have the following template: <xsl:template name=theday> <xsl:param name=thisday /> <xsl:variable name='holiday' select='foo'/><!-- made
I've made a small example with a Person class, which contains a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.