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 7635199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:22:45+00:00 2026-05-31T07:22:45+00:00

I have made a few linked lists before and the concepts all make sense

  • 0

I have made a few linked lists before and the concepts all make sense to me, but for a project I have to make a templated one in C++, which I haven’t used a whole lot, and I am having some trouble. Please help. I have spent far too much time on such a simple thing.

I have some of the list class already, but the problem lies somewhere in here. When I make three nodes in a test case and link them all up, if I call

node1.getNext().show();

That works fine, but if I

node1.getNext().getNext().show();

I get a segfault (core dumped). What is wrong here? I have tried changing the pointers on the return values of getNext() and getPrev() a bunch of times with no luck. I feel stupid asking this question, but I’m having some serious trouble. My node class is below, followed by a sample test case that gives a segfault.

Node.h:

template <class T> class Node
{
 public:
  Node();
  Node(T value);
  void setPrev(Node<T> node);
  void setValue(T value);
  void setNext(Node<T> node);
  Node<T> getPrev();
  T getValue();
  Node<T> getNext();
  void show();
  ~Node() { }

 private:
  Node<T> *prev;
  Node<T> *next;
  T value;
};

//default construct                                                                                                                                                     
template <class T> Node<T>::Node() {
  this->prev = NULL;
  this->value = NULL;
  this->next = NULL;
};

//overloaded construct                                                                                                                                                  
template <class T> Node<T>::Node(T value) {
  this->prev = NULL;
  this->value = value;
  this->next = NULL;
}

template <class T> void Node<T>::setPrev(Node<T> node) {
  this->prev = &node;
}

template <class T> void Node<T>::setValue(T value) {
  this->value = value;
}

template <class T> void Node<T>::setNext(Node<T> node) {
  this->next = &node;
}

template <class T> Node<T> Node<T>::getPrev() {
  return this->prev;
}

template <class T> T Node<T>::getValue() {
  return this->value;
}

template <class T> Node<T> Node<T>::getNext() {
  return this->next;
}

template <class T>
void Node<T>::show() {
  cout << value << endl;
}

Test case:

int main(int argc, char **argv) {

  typedef Node<int> IntNode;

  IntNode head(NULL);
  IntNode node1(23);
  IntNode node2(45);
  IntNode node3(77);
  IntNode tail(NULL);
  node1.setPrev(head);
  node1.setNext(node2);
  node2.setPrev(node1);
  node2.setNext(node3);
  node3.setPrev(node2);
  node3.setNext(tail);

  node1.show();
  node2.show();
  node3.show();

  cout << node1.getNext().getValue() << endl;
  cout << node1.getNext().getNext().getValue() << endl;
}
  • 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-31T07:22:46+00:00Added an answer on May 31, 2026 at 7:22 am

    You need to pass values by reference rather than by value.

    When your node1.setNext(node1) is executed, setNext() gets a copy of node1 on the stack, not the variable you define in main(). Once setNext() exits, the address stored in next is no longer valid.

    For starters, redefine functions setPrev and setNext as

    template <class T> void Node<T>::setPrev(Node<T> &node) {
      this->prev = &node;
    }
    
    template <class T> void Node<T>::setNext(Node<T> &node) {
      this->next = &node;
    }
    

    and that will fix your immediate segfaults. However, you really need to think more about design of your code and what happens when you pass by reference or by value, to write this correctly and avoid further problems.

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

Sidebar

Related Questions

I am not a seasoned SSRS veteran. I have made quite a few but
I made a similar question a few days ago, but now I have new
I'm new to the Windows Mobile scene, but I have made a few Symbian
I have made few changes on this huge JSF page, which is full of
i have made a few multi activity database applications using sqlite, but i get
I have made multiple simple but fun apps for iPhone in the past few
I have a database backup which I imported into SQL Server and made few
I have just started programming and have made a few small applications in C
I have made up a few different custom cells in my UITableView in Interface
I have made a list with few elements in it. Now everything looks right,

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.