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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:29:34+00:00 2026-05-13T23:29:34+00:00

Hi I was working on a bit of fun, making an interface to run

  • 0

Hi I was working on a bit of fun, making an interface to run gnuplot from within c++, and for some reason the my linked list implementation fails.

The code below fails on the line plots->append(&plot). Stepping through the code I discovered that for some reason the destructor ~John() is called immediately after the constructor John(), and I cannot seem to figure out why.

The code included below is a stripped down version operating only on Plot*. Originally I made the linked list as a template class. And it worked fine as ll<int> and ll<char*> but for some reason it fails as ll<Plot*>.

Could youp please help me figure out why it fails? and perhaps help me understand how to make it work?

In advance: Thanks a heap!

//B2S

#include <string.h>

class Plot{
  char title[80];
public:
  Plot(){  }
};

class Link{
  Plot* element;
  Link* next;
  Link* prev;
  friend class ll;
};

class ll{
  Link* head;
  Link* tail;
public:
  ll(){
    head = tail = new Link();
    head->prev = tail->prev = head->next = tail->next = head;
  }
  ~ll(){
    while (head!=tail){
      tail = tail->prev;
      delete tail->next;
    }
    delete head;
  }
  void append(Plot* element){
    tail->element = element;
    tail->next = new Link();
    tail->next->prev = tail;
    tail->next = tail;
  }
};

class John{
  ll* plots;
public:
  John(){
   plots= new ll();
  }
  ~John(){
    delete plots;
  }
  John(Plot* plot){
    John();
    plots->append(plot);
  }
};   

int main(){
  Plot p;
  John k(&p);
}
  • 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-13T23:29:34+00:00Added an answer on May 13, 2026 at 11:29 pm

    The statement:

     John();
    

    Just constructs a new nameless John which is created and immediately destroyed. There is no way (in the current version of C++) to call a constructor from another constructor or on an already constructed object.

    This means that the plots member of your John called k is never being initalized, hence the crash when you call append from the John constructor.

    [As Neil Butterworth comments, if you had actually constructed an ll instance there are other issues with it, this is just the most immediate problem.]

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

Sidebar

Related Questions

Working with a bit of in-line CSS within a CMS (fun!). This chunk of
I was working a bit ahead and planning for transition from EF 4.2 CTP
Language : C++ I am working on Bit Packing (Extracting the required bits from
I'm working on some CouchDB apps and it's been lots of fun. I'm also
I'm working on a bit of a project in Python. I have a list
I am currently working on a bit of code to retrieve information from a
I'm working on a bit of code for my C++ class, and I'm a
I've started working a little bit with lift+scala+mongorecord but I found a small annoyance
I've been slowly and a bit painfully working my way up the datastore/JDO learning
I was wondering if someone can help me out - working on a bit

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.