0

I am attempting to assign a pointer to a value like so

    string name;
    XMLSerial * ptr;

    if(name == "Armor")
    {
         ptr = &name;
    }

Name is initialized earlier. XMLSerial is a class that I have written. I am trying to deserialize some XML into objects. If the name is one of the classes or objects that I have defined, I want the XMLSerial object pointer to create space for the object.

The error i'm getting is cannot convert std::string* {aka std::basic_string<char>*} to XMLSerial* in assignment.

4
  • 3
    What do you expect the above code to do? Why would you want to point to a string with a pointer to a fundamentally different type? Commented Feb 11, 2013 at 0:24
  • You can't deserialise XML just by assigning a pointer T1* to a pointer T2*. Commented Feb 11, 2013 at 0:25
  • You should explain to us why you think it should compile, because it seems obviously wrong to me. Commented Feb 11, 2013 at 0:44
  • Oh I know why it won't compile. I understand the error, I just don't know how to fix it. I was trying to avoid posting a mile of code. Before this code, the program reads in the xml file one char at a time and then once it hits the end bracket, the name value is set. Then I need this pointer to compare the name to the names of the classes I have to handle and construct a new one if the name matches, assigning it to the pointer I passed in to the function. Commented Feb 11, 2013 at 1:03

2 Answers 2

1

The code you posted should fail exactly in the way that you describe. It is difficult to know exactly where you've gone off the rails, but your XMLSerial might have an operator=(std::string) that you are trying to use.

If that is the case, replace your code with:

string name;
XMLSerial xml;

if(name == "Armor")
{
     xml = name;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that your XMLSerial object can be constructed from a string, this will work.

string name;
XMLSerial * ptr;

if(name == "Armor")
{
     ptr = new XMLSerial( name );
}

Edit:

And, as commented, dealing with pointers and new/delete come with their own responsibilities.

1 Comment

Let's be careful about throwing new out there without further information, in this case, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.