Thanks for reading my question. Working in C++ and on Visual Studio 2010. I am writing a DnD roster program for the summer to keep myself in the know on all the programming skills I've already learned (learned up to pointers, dynamic memory, file processing, and classes). I have a class and inside the class I have a struct declared called charNode for a linked list. I am trying to create a micro-function that creates a new charNode and initializes the necessary info, then returns the pointer to the new node. Is this possible to do? Example of the code below.
In the .h file:
charNode* createNewCharNode();
In the implementation file:
1 2 3 4 5 6 7 8 9 10 11 12
charNode* DnDChar_3_5::createNewCharNode() {
charNode* newNodePtr;
newNodePtr = new charNode;
// All of these are part of the struct and cstring is #include'd
newNodePtr->Name[0] = '\0';
newNodePtr->Race[0] = '\0';
newNodePtr->Class[0] = '\0';
newNodePtr->CreationType[0] = '\0';
return newNodePtr;
}
I get this error in the implementation file - IntelliSense: identifier "charNode" is undefined
Is it possible to return this pointer or would I just need to put this section of code in each time I wanted to create a new node for the linked list?
I understand the main thrust of your piece of code, but am a little confused as to how it fits together. As part of the struct I have a function that allocates the info needed and just call it when I make a new struct?
Am I not able to have that function outside of the struct? Because that would seem wasteful, space wise, to have that inside every struct.
I think what I am trying to do is make
test * newtest() { return new test(); }
but my compiler is not recognizing that "test*" is a valid statement to return?
I have the struct header declared inside the class. Moving it outside the class while still in the class header file will solve the problem? My main computer is in use right now, which is why I'm asking rather than testing myself.