c++ - I assign a pointer and it nulls itself out -
so have constructor object, on creation sets few values , places @ end of linked list.
the problem i'm having when assigns address of new object head or tail of list, assigns, leaves constructor , reason head , tail both reset 0.
object object1("oddjob", 2, 2, 9);
calls constructor
object::object(string label, float x, float y, float z) { x_ = x; y_ = y; z_ = z; if(label == "") { label = "object"; } label_ = label; if(headobject == 0) { headobject = this; tailobject = this; } else { tailobject->next = this; tailobject = this; } next = 0; }
edit: headobject , tailobject globals declared in .h file. declared as:
static object * headobject; static object * tailobject;
the use of static
on global object causes have internal linkage. means each translation unit includes header have own version of headobject
, tailobject
.
instead, should declare them extern
in header file:
extern object * headobject; extern object * tailobject;
then in single translation unit (typically .cpp
corresponding .h
), should give definitions as:
object * headobject; object * tailobject;
you can't define them in header file, otherwise you'll break 1 definition rule when include header in multiple files.
Comments
Post a Comment