I am using C # for one year and recently my patience with C ++'s harsh world I'm testing.
I am trying to create an object focused binary tree. I have stepped through the code and read on using reference parameter passing and const in C ++, but what I am doing to cause an entry violation error I have ensured that the structure is created properly And completes the expected main line of code, although calling a string causes an error and why I can not work.
Even the code is:
// ExpressionCL.cpp: determines the entry point for the console application // include the "stdafx.h" namespace std Using; Template & lt; Class TData & gt; Class triode {Private: Teadata data; Constantennode & lt; TData & gt; * Left = nullptr; Constantennode & lt; TData & gt; * Correct = nullptr; Zero set data (tudata data) {data = data; } Public: TriNode & lt; TData & gt; (Tudata data) {set data (data); } Treeode & lt; TData & gt; (Tudata data, Constantonnode & lt; tidata & gt; left node, const trionode & lt; tidata & gt; and writenode) {setdata (data); SetLeft (leftNode); SetRight (rightNode); } Zero set-left (Constant node & lt; TData & gt; & left node) {Left = & amp; LeftNode; } Zero Satrite (Constantennode & lt; TData & gt; & amp; Rytonnode) {correct = & amp; RightNode; } Treeode & lt; TData & gt; GetLeft () CONST {if (left);}} TreeNode GetRight () console {if (Haritite)} {return right;}} TData getData () const {Return data ;} Bool hasLeft () const {if (Left! = Nullptr) {return true;} Other {return false;}} Bool hasRight () const {if (right! = Nullptr) {true;} Other {return false;} } String toString () const {string treeString = ""; If (left ()) {treeString + = Left-> toString ();} TreeString + = to_string (data); if (harrett) {treeString + = Right-> toString ();} returning treestring;}}; int _tmain (int argc, _TCHAR * argv []) {tree_node> int> intTree (1, tree_node> int> (1 ), Triode & lt; int & gt; (2)); Cout &
Some guidance or more recommended resources will be great.
Set your setLeft
and setRight
function alarms bells Anyone passed by reference Storing the address of the object is seriously asking because the caller can destroy the object, and then you can delete Left
and Right
.
In fact it's okay what you do. You pass a temporary object to your constructor, store their address in left
and right
. Then you call the IntTree.toString ()
which tries to use the pointer for the objects that no longer exist.
To fix this, you need to use manual lifetime management. Your nodes means that the node should be created through new
. You have the option of using either raw pointers (in this case, you should carefully note your interface that the caller should call new
, pass in the pointer, and Do not call
later).
The second option is to use Smart Pointers which will track the ownership of the items, although before doing so you have to solve some other problems.
Specifically, it is extremely important to decide whether code> pen node does not currently follow this. At least, so that you do not accidentally duplicate a treeNode
(which will not behave properly unless you follow the rule of three)).
Using Smart Pointer Classes means that you can follow the three rules instead of the rule which creates a very cleaner code (although if you are new to C ++, then directly with the bat It's hard not to have any good online learning resources which I know besides this).
No comments:
Post a Comment