string variable cannot be assigned

Hi,
I am using a string inside a structure like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct action
{
	enum actionCommand
	{
		DELETE,
		CHGE_ELEM_VALUE,
		REPLACE_LINKID,
	};
	actionCommand command;
	string commandText;
	action *nextAction;
	action *previousAction;
};


now in a function where I have a pointer to the structure I am doing this:

 
currAction->commandText="String";


But this doesn't work.

I have tried this also:

 
currAction->commandText.assign("String");


But this doesn't work either. Please help me to understand this problem and how can I get around this.

Any help will be appreciated. Thanks.
Looks no problem.

What do you mean it doesn't work? compile error or anything else?
What error does it gives?
Thanks for your reply.
It crashes when the program is running. And if I go into debug mode this is the step that causes it to crash.
Well, sounds like memory allocation problem. Paste the code where you have allocated memory for currAction.
Thanks,
I found the issue. Actually I was declaring currAction as:

1
2
3

action * currAction;


When I changed it to:

1
2
3

action * currAction = new action;


Its working now.
I hope you realize that it is working because you allocated memory for currAction. There is nothing wrong with declaring currAction like you were doing before. It's just that you HAVE to allocate memory before dereferencing a currAction.

1
2
3
4
5
6
7
8
9

action *currAction ; // fine 

currAction->commandText = "String" ; // problem, since the memory for currAction is not allocated. 

currAction = new action; // memory for currAction allocated 

currAction->commandText = "String"; // now OK


Hope this helps!
Last edited on
Thanks kevin,
That helps a lot. I understand what was going on now.
Topic archived. No new replies allowed.