5 Replies - 5245 Views - Last Post: 23 May 2011 - 07:45 PM Rate Topic: -----

#1 bassoon15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-May 11

linked list error: std:: logic_error

Posted 23 May 2011 - 12:50 PM

Hello all!

I am currently working on a linked list mini database project. I have written all my functions, but when I go to compile, I get the error given below. I am not sure where to start looking in my code for the error, or what exactly it means, that is what I did not attach any code excerpts. Could anyone offer some pointers? (no pun intended)

Thanks much!


terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid
Abort (core dumped)
 


Is This A Good Question/Topic? 0
  • +

Replies To: linked list error: std:: logic_error

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: linked list error: std:: logic_error

Posted 23 May 2011 - 01:07 PM

it's one of the exceptions that the standard throws.

the exception is telling you you passed a null reference to a string constructor.

have a look
Was This Post Helpful? 1
  • +
  • -

#3 bassoon15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-May 11

Re: linked list error: std:: logic_error

Posted 23 May 2011 - 01:53 PM

Thanks, that is helpful!
I still have some things that are unclear though.

I have found the line where the error lies.

In theory: in the code below, the user will be prompted to enter a name. once entered, this input will be compared with all the names of the nodes in the list. If found, the name and all corresponding information in that node will be removed from the list.

void bdaybook::remove_bday(){ //remove a birthday by name

string target; //where the previous logic error was given. 

//link is defined in the struct node as node *link;


if((head -> link != NULL)&&(head -> name != target)) { //this but be the null being complained about.
   
    node *before = head; //pointer to node before the one being removed
    node *remove = before -> link; //pointer to node being removed.
    node *after = remove -> link;  //pointer to node after the one being removed.
    while((remove -> name != target)&&(remove -> link != NULL)) {
      before = before -> link;
      remove = remove -> link;
      after = after -> link;
    }
    
    if(remove -> name == target) {
      before -> link = after;       //traverse the list, decrement the count for the number of nodes, and move the pointers respectively
      delete remove;
      count--; 
      
      cout<<"Birthday has been removed from database"<<endl;
    }
  }
  
  if(head -> name == target) { //Removing head
    node *before; //Node before target
    before = head;
    head = head -> link;
    delete before;
    count--;
    cout<<"birthday has been removed from database"<<endl;
  }

  //return 0;

cout<<"birthday has been removed from database"<<endl;


}                 




Perhaps I should try another method.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: linked list error: std:: logic_error

Posted 23 May 2011 - 01:56 PM

string target; //where the previous logic error was given. 

if((head -> link != NULL)&&(head -> name != target))


target has no value! Also, you should check to see if head is NULL before you access it.
Was This Post Helpful? 1
  • +
  • -

#5 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: linked list error: std:: logic_error

Posted 23 May 2011 - 02:00 PM

Have you thought that it might be because you are trying to compare a string object with an uninitialized string object?
Also, you should be using a different function such as string::compare(string) to check make a comparison
Was This Post Helpful? 1
  • +
  • -

#6 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: linked list error: std:: logic_error

Posted 23 May 2011 - 07:45 PM

i think he is constructing a string with a NULL pointer. thats what the exception says at least.

edit: run this code, it dose the exact same thing.

#include <iostream>
#include <string>
#include <exception>

int main() {
	try {
		std::string badstr(NULL);
	} catch(std::exception& e) {
		std::cout<<e.what();
	}
}



you need to look for mixing char* and std::string, that is where your error is occurring.

my guess is that node::name is a char*

This post has been edited by ishkabible: 23 May 2011 - 07:52 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1