![]() |
| ||
| Graph help please This grap is kicking my butt. I am not sure how to do it. Can someone please help explain these to me. // graph.h This is what I have so far but I don't think this is right. Can someone please explain these functions I just don't get it. #include <iostream> |
| ||
| Re: Graph help please It might help if you explain what your Graph-class is supposed to do. By the way, what is your constructor definition supposed to do? You assign numV to be 0 then you de-reference an uninitialized array of vertices. You probably meant to do something like this-- Graph::Graph(int num) : numV(num), vertices(new VertexNode[num]){-- the numV looks like it represents the total number of VertexNodes (or vertices) and it is initialized via pre-initialization, along with your pointer to VertextNodes. The memset in the constructor body is necessary because by calling new you are invoking the default constructor of an instance of a class ( or struct or union in C++ ) in which it is typically the responsibility of the that constructor to initialize members of the object. However, because your struct of type VertexNode has no constructor you have to explicitly set the values inside the struct. A good way of doing so is by using memset. In the above code the memset sets the bit-value of every byte for each VertexNode to zero, so everything has a value of 0 in your struct. So this means... struct VertexNode // Structure representing a vertex *string's members are set to zero (the backing char-pointer a string has is likely to point to null), as well as any other members in the string object. *bool is set to false (because 0 is false and any other number is true) *EdgeNode points to null (size of pointer is typically 4-8 bytes, depending on the machine (might be more on some machines but lets assume 8 bytes), so lets assume each byte's character determines where the EdgeNode is pointing to. If the bytes are set to zero, then the only location EdgeNode is pointing to is NULL, or location 0x00000000 where each 0 (except the first one followed by an x) represents the value of the location in bytes via hexidecimal). This should happen to each element in your array sense memory is being set for not just one VertexNode, but the amount that is pre-initialized. That's probably overkill for an explanation of the choice of initialization for your array of values but hopefully it helps @_@. Note: It may be better to migrate the initialization of your numV and VertexNode pointer to the body of the constructor so you can catch an out-of-memory error. This way if an error occurs and memory cant be assigned to the VertexNode pointer, your application wont crash for attempting to 'initialize' memory that may not belong to it. Linkage |
| ||
| Re: Graph help please Sorry to be so vague but here are my instructions. I understand what a graph is but I just dont know how to impliment it. Your main function must perform all of the following tasks. |
| All times are GMT -4. The time now is 3:41 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC