Dear All.

I have a small problem with the code attached. I am trying to add an object to a linked list. When I try to compile I am getting an error. For the life of me I cant figure out the error. this is after 5hrs of head wrecking searching. Any help would be greatly appreciated. thx in advance Tim.

Dani AI

Generated

Brief summary: reported a compile-time problem when adding to a linked list. correctly flagged a logic hole where an invalid index can still cause a null-pointer dereference. called out a classic compile-time culprit (a missing semicolon after a typedef/class) and pointed out mismatched return types between a declaration and its definition. Either kind of error can produce cascade compiler messages that obscure the real root cause.

Checklist of concrete items to inspect (work top-to-bottom through the files):

  • Read the first compiler error; later messages are often side effects.
  • Verify every typedef, struct and class declaration ends with a semicolon.
  • Confirm function signatures in headers exactly match their cpp definitions (return type, pointer vs non-pointer, const, parameter types).
  • Ensure find() is declared to return a pointer and that callers check for a null result before dereferencing.
  • Decide and document whether the list is 0-based or 1-based and keep size(), find() and all callers consistent.
  • Guard headers and include the file that defines SeqItemType before using it.

A safer retrieve pattern avoids returning a raw item and silently dereferencing a null node. For example:

bool Sequence::get(int index, SeqItemType &out)
{
    if (index < 1 || index > size()) return false;
    ListNode *node = find(index);
    if (node == 0) return false;
    out = node->item;
    return true;
}

Practical tips: compile with warnings enabled (e.g., -Wall -Wextra), fix the first error, and then recompile. Use simple grep searches for SeqItemType and find( to spot mismatches. Applying ’s null-check advice plus ’s header-signature/semicolon checks will usually resolve these kinds of compile/runtime failures.

Recommended Answers

All 2 Replies

Tim,

I only time to look at one file. In Sequence.cpp I found this function. I believe this may cause a problem if the index is not valid.

SeqItemType Sequence::retrieve(int index)
{
  if ((index < 1) || (index > size())){
    // can't delete something that isn't there
  }
  ListNode *cur = find(index);
  return cur->item;
}

If the expression in the if statement is true it will still do the last bit of code, unless you wanted and if-else structure.
The find(index) will return zero if index is not valid. Then when it tries to resolve cur->item, that might give you a problem. Look at your code and see if you are ever returning 0 that will be used in a cur->item context. Check for 0. If it is, return or move on without doing anything.

One would be the missing semicolon in "Sequence.h":

typedef patient SeqItemType;

Others are things like returning a pointer from a function that is not declared as returning a pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.