0

Can someone tell me why when my split function runs it makes the list2 of the object lst2 read "Error reading characters of string"

LinkedDeque* LinkedDeque::split()
{
    LinkedDeque* lst2 = new LinkedDeque();
    DLinkedList list2 = *(D.split());
    lst2->list2 = list2;
    return lst2;
}
void LinkedDeque::print() {
    
    D.print();
}
void LinkedDeque::splitprint() {
    list2.print();
}

int main()
{
    LinkedDeque list;
    while (true)
    {
        string x;
        cout << "Enter a non-negative number (Negative number to end): ";
        cin >> x;
        cout << endl;
        if (stoi(x) < 0)
            break;
        list.insertFront(x);
    }

    cout << "Entries Given:\n";
    list.print();

    cout << "Splitting the given list ...\n";
    LinkedDeque lst2 = *(list.split());
    
    cout << "\n1st Split List : \n";
    list.print();
    cout << "2nd Split List : \n";
    lst2.splitprint();

    return EXIT_SUCCESS;

}

If more code is needed to tell me why this is going on I can provide the full code of the program I am working on. I am clearly not the best at programing so please explain it so I can understand. I have been working on this for 3 days now and cannot figure out why this is happening.

KNG
  • 1
  • 2
  • What is `"Error reading characters of string"`? Is it an error message? Where is that printed? I don't see it in the code you have provided. – Etienne de Martel Feb 21 '22 at 03:26
  • *I am clearly not the best at programing / I have been working on this for 3 days now* -- [What is a debugger, and how can it help diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Using a debugger is part and parcel of learning how to write programs. Using a debugger, you may have solved the problem within minutes, not days. Also, isn't this a little bit advanced (creating a linked list / deque) for a beginner or fairly new C++ programmer? – PaulMcKenzie Feb 21 '22 at 03:28
  • This is for a Data Structures class that I need for my college even though the jobs I'm going to be applying for outside of college are going to be networking and not programming. I have tried to use the debugger but do not see where the error is even occurring as when it goes into the split function it is fine, and it isn't until it comes back that the error is happening. when it comes back from the function and goes to the line "LinkedDeque lst2 = *(list.split());" it says that lst2's list2 has been set to "" @PaulMcKenzie – KNG Feb 21 '22 at 03:46
  • @EtiennedeMartel The error is that when executing the code immediately after the line "LinkedDeque lst2 = *(list.split());" is run it sets the lst2's list2 member to "" and I do not know how to fix that. – KNG Feb 21 '22 at 03:51
  • If *the debugger* says "Error reading characters of string", it is because you have an invalid pointer somewhere (it's not the value of the string, it's an error message). My first guesses would be a violation of the rule of three, the `split` function of whatever `D` is, or a broken implementation of the linked lists. – molbdnilo Feb 21 '22 at 07:47

0 Answers0