Class function not behaving as I wanted

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?

Thanks in advance!

EDIT: misspelled "identifier".
Last edited on
do you mean something like that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std;

struct test
{
	test()
	{
		a='x';
		b=232;
	}
	char a;
	int b;
};

class asd
{
private:
	test * p_test;
	test * newtest()
	{
		return new test();
	}
public:
	void printtest()
	{
		cout << p_test->a << endl << p_test->b << endl;
	}
	asd()
	{
		p_test = newtest();
	}
	~asd() 
	{
		delete p_test;
	}
};

int main()
{
	asd * p_asd = new asd();

	p_asd->printtest();

	delete p_asd;

	system("pause");
	return 0;
}


edit. maybe change newNodePtr = new charNode; should be newNodePtr = new charNode();
Last edited on
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?
It seems that the compiler does not see the definition of test. You should include the corresponding header in this module.
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.
Topic archived. No new replies allowed.