existing string to name a struct

Hey All,

I have an existing string.

string stateString;

I have a struct.

struct node
{
int nodeState[];
int d;
int h;
int c;
};

I have a queue

queue <node> fringe;

I would like to create my structs using whatever the value is in stateString at the time.

I typed in

node stateString;

then made a for loop to fill in the nodeState array

then assigned the valued to the variables

stateString.d = d+1;
stateString.h = h;
stateString.c = d+h;


I would then like to push the struct into a queue.

Here I am lost.

if I use

fringe.push(stateString)

I get an error.

just a red line under the . between fringe and push. It seems to think I am wanting to use the string stateString.

Does anyone know the proper way to use the name currently stored in the string stateString as the name for the new struct?

Let me know if something is not clear. Also If I am over complicating something I am up for alternate way of doing it.
Why are you giving your std::string and your node instances the same name as each other?
1
2
string stateString; //"stateString" now used up
node stateString; //what?? "stateString" is already used, this doesn't compile 


Edit: Ah, you seem to be confused - the name of the node is not remembered by the queue, it is just a way to tell apart that specific struct from another in your programming, and it is not considered at all when you actually run the program. Variable names are meaningless and are only there for us humans.

Perhaps you want a std::map<std::string, node> instead?
1
2
3
std::map<std::string, node> MyMap;
node MyNode;
MyMap["Some String or name, for instance"] = MyNode;
Last edited on
That was my attempt to create a new struct with the value that was stored in stateString. Obviously it did not work. That is why I am asking people here how to do it. Do you have a solution or are you just curious?
Your node class has no way to store any kind of string, why would you expect it to?

For reference, it is not that I am curious; I am trying to ask you questions that lead to you understanding your mistake on your own. I cannot tell you what you are misunderstanding because I do not know, and you can't tell us because you don't know either until after you actually understand it.

And I provided a possible solution, but you must have seen my post before I edited it in.
Last edited on
That was my attempt to create a new struct with the value that was stored in stateString.

What do you mean by that? stateString is a string. A node is a structure containing an array of integers and some more integers. How can the structure have the value that was stored in the string, when they are of different types?

Edit: In any case, declaring two objects with the same name does not cause the second one to contain the same value as the first, even if they do have the same type. What you want to do is create a second object with a different name, and then assign to it the value of the first:

1
2
string firstString = "Some text";
string secondString = firstString;

Last edited on
If your string contains data about what the structure should contain, you'll need to use std::istringstream to extract that data, or you shouldn't store that data in a string in the first place.
Your edited post makes sense. You are correct in that I replied before you added that information.

So from what I am understanding is that I should just store the contents of the string inside the node the same way the array and the three ints are stored

node newnode
{
string nodeString = statestring;
int nodeState[9]; (for loop to assign values)

etc
};

Is that a correct understanding?

Thanks for offering the map solution but I just need to get this into a queue.

Another process is going to read the nodes out of the queue in FIFO. I do not control it.

You need to modify your definition of your node struct.

This is code from your first post:
1
2
3
4
5
6
7
struct node
{
    int nodeState[];
    int d;
    int h;
    int c;
};
You can add the ability to contain a std::string like this:
1
2
3
4
5
6
7
8
struct node
{
    int nodeState[];
    int d;
    int h;
    int c;
    std::string state;
};
Then, when you make an instance of your node, you can create it one of these two ways:
1
2
3
4
5
6
node MyFirstNode; //make a node called MyFirstNode
MyFirstNode.nodeState = /*something*/;
MyFirstNode.d = /*something*/;
MyFirstNode.h = /*something*/;
MyFirstNode.c = /*something*/;
MyFirstNode.state = "something"; //it's a string 


Then you can add it to your queue as you originally tried.

I'm not sure what your plan is with the int array nodeState; because it is in a struct and you gave it no specific size, it has degraded to only being an integer pointer and not an actual array. You would probably have to use dynamic memory for that, which involves new and delete. I'm guessing you don't actually need this array, though, and that it was just your attempt to put the string into the node.
Last edited on
The size of the array in the struct is [9] and they are set with a for loop just copying from another array. That part is working.

I have moved the string inside the node.

where you have

node MyFirstNode

this function will be called a lot. Does it matter what is in the place of

MyFirstNode

where you have

MyFirstNode.state = "something";

This is where I need the value of what is stored in stateString.

MyFirstNode.state = stateString

maybe?

bwquestion wrote:
The size of the array in the struct is [9] and they are set with a for loop just copying from another array. That part is working.
No, it is not. You cannot 'set' the size of that array - it isn't actually an array, it is a pointer to some random memory address, and you are copying over that memory with your own numbers (bad!!).
bwquestion wrote:
where you have

node MyFirstNode

this function will be called a lot.
It's not a function, it's just creating an instance of your node struct on the stack. It should be reasonably fast.
bwquestion wrote:
Does it matter what is in the place of

MyFirstNode
No, you can use any name you want, as long as it is not already used. In some cases you can use a name already in use but it almost always leads to problems with ambiguity.
bwquestion wrote:
where you have

MyFirstNode.state = "something";

This is where I need the value of what is stored in stateString.

MyFirstNode.state = stateString

maybe?
Yep, that's right, assuming you include a semicolon at the end.
Last edited on
Thanks for your help the program is working.

A follow up question.

If I declare an array of size 9 inside the struct

struct node
{
string nodeString;
int nodeState[9];
int d;
int h;
int c;
};

why would it not be an array and why would it be bad to write to it? The documentation on this seemed to be very clear and it works just as I thought it should.

For clarification

This piece of code is inside a function that is being called a lot.

If you declare its size there, then it is an array.

This struct has a pointer, not an array:
1
2
3
4
5
struct MyStruct
{
    int MyArray[]; //NOT an array!
};
//sizeof(MyStruct) == sizeof(int *) 
Whereas this struct does have an array and not a pointer:
1
2
3
4
5
struct MyStruct
{
    int MyArray[10];
};
//sizeof(MyStruct) == sizeof(int)*10 


If you always specify the number, it will be an array stored within the structure, and there is nothing special to do. If you don't specify a size it degrades into a pointer automatically, and you have to use dynamic memory management.
Last edited on
Thanks again for your help with this. Your information and patience is appreciated.
Topic archived. No new replies allowed.