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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:06:38+00:00 2026-06-18T13:06:38+00:00

I am having problems making a copy constructor. Consider the code below: In List.h

  • 0

I am having problems making a copy constructor.
Consider the code below:

In List.h

template <class T>
struct ListNode
{
    T value;
    ListNode<T> *next;
    ListNode<T> *prev;

    ListNode(T theVal)
    {
        this->value = theVal;
        this->next = NULL;
        this->prev = NULL;
    }
};

template <class T>
class List
{
    ListNode<T> *head;

public:
    List();
    List(const List<T>& otherList); // Copy Constructor.
    ~List();
    };

In list.cpp

template <class T>
List<T>::List()
{
    head=NULL;
}
template <class T>
List<T>::~List()
{
}
template <class T>
List<T>::List(const List<T>& otherList)
{
}

// I have google the problem. Concept is simple. Make a new head and assign its node the //values of old List node.
// So for I have tried the following.

ListNode<T> *old = head; // pointer to old list.
ListNode<T> *new;// pointer to new head.

while (old->next!=NULL){
new->value = old->value;
old = old->next; 
}

// The only problem is how to make a new head that would point to my new Copied list.

  • 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-06-18T13:06:39+00:00Added an answer on June 18, 2026 at 1:06 pm

    This whole question is unclear, and there are a number of problems with the code, including a useless destructor, and not copy assignment operator.

    You can’t generally define templates in a .cpp file, the entire template definition must be visible to all users of the code, which generally means defining the entire template in header files.

    ListNode<T> *new;// pointer to new head.
    

    new is a keyword in C++, you can’t use it for variable names.

    // The only problem is how to make a new head that would point to my new Copied list.

    What copied list? You haven’t actually copied anything or created any new nodes.

    You need to create copies of the nodes in otherList

    template <class T>
    List<T>::List(const List<T>& otherList)
    {
      ListNode<T>* node = otherList.head;
      ListNode<T>** tail = &head;
      while (node)
      {
        *tail = new ListNode<T>(node->value);
        tail = &tail->next;
        node = node->next;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems making @Column(name=example) working. I've got a User class: @Entity public class
I am serializing a Multidimensional list using JSON.Net, and I am having problems making
I'm having problems making Lua & Luabind work under Windows 7 using VS 2010.
I am not using GraphViz because I am having problems with making it play
I'm having some problems with making my tests insert fake data in my database.
I am currently making a facebook app and am having some problems with permissions.
Im having problems with a method in a class file: public static function getPageLeft($pid)
Hi I'm having some problems making my function structure. Would appreciate any help. First
I am programmatically building a UINavigationContoller for iOS and am having problems making it
I am having some problems making twitter bootstrap drop-down navbar work. I ran through

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.