Question

I am sure that for most this is a very easy question. But I am writing a token recoginzer for XML in c++ and I am using a stack to make sure there are matching begining and end tags. Well my tags are c strings...

char BeginTag[MAX];

I am trying to push that onto my template stack. But I am unsure what type to pass the stack. I have tried...

stack<char> TagStack;

But that doesn't work. I have tried a few other solutions alos but none seem to work. Can someone help me?

Was it helpful?

Solution

Arrays aren't assignable, so can't be used as a container value type.

You could define a struct containing an array, though, and use that:

struct Tag {
    char name[MAX];
};

stack<Tag> TagStack;

Or just use a std::string for your tags.

OTHER TIPS

It'd help if you posted the code that doesn't work, and tell us how it doesn't work. (Compile-time error? Runtime error?) But my suggestion would be to use std::string, at least on the stack:

using namespace std;
stack<string> TagStack;

You should be able to push onto the stack without an explict cast:

TagStack.push(BeginTag);

Note: I don't endorse your use of C strings for this purpose; I'd use std::string in the tokenizer also. But that's your call. If you continue using char arrays, you might want to change char[MAX] to char[MAX+1], as MAX would normally be used to denote the maximum number of non-null characters in the string. Hence, you need to ensure that there's one extra char allocated for the terminating null. This may be merely a style issue, but it may also help prevent bugs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top