Hello! First of all, excuse me for my bad english!
Im new in C++ (altough i have to implement a mix between c and c++) and im having troubles trying to do a few things...

struct nodo {
    Program * prog; 
    int FB;
    nodo * izq;
    nodo * der;
}

typedef nodo* AVLProg;

AVLProg emptyAVLProg() {
    return NULL;
}

AVLProg NodeAVLProg(Programa * prog) {
    AVLProg a = new NodeAVLProg;
    a->prog = new Programa;
    a->prog = prog;
    a->FB = 0;
    a->izq = emptyAVLProg();
    a->der = emptyAVLProg();
    return a;
}

For some reason my program explotes when it gets to "AVLProg a = new NodeAVLProg" in the NodeAVLProg functions, and i dont know why!! Please heeeelp!!! Thanks in advance!! =)

Recommended Answers

All 9 Replies

Where it says programa" it should be program... in the code is fine, i write it wrong here

Please mark the thread as solved if you're all set.

Please mark the thread as solved if you're all set.

Oh noo.. i still get the same problem, i only wanted to point that i write wrong the name of the data type but in my code it's fine... hehe

Please, make it clear what you mean by explotes.

What this code does if you like.
This will help in testing it in a better way.

Member Avatar for embooglement

This line is wrong: "AVLProg a = new NodeAVLProg;" NodeAVLProg is the function you're using, not a type. It should be "AVLProg a = new AVLProg;" There's one more issue you've got that won't give you any errors, and that is this:

a->prog = new Program;
a->prog = prog;

new is creating an instance of Program, and returning a pointer to it. However, you immediately copy over that pointer with the one passed as an argument. If you wanted a->prog to point to the same thing as the argument, then the first line is unnecessary. If you wanted a->prog to copy the thing pointed to by the argument, you need to dereference both pointers, like so: "*a->prog = *prog;"

Also make sure that whenever you use new you also use delete to get rid of the allocated memory, which with your current code you couldn't do because you lose the pointer given to you by new.

This line is wrong: "AVLProg a = new NodeAVLProg;" NodeAVLProg is the function you're using, not a type. It should be "AVLProg a = new AVLProg;" There's one more issue you've got that won't give you any errors, and that is this:

a->prog = new Program;
a->prog = prog;

new is creating an instance of Program, and returning a pointer to it. However, you immediately copy over that pointer with the one passed as an argument. If you wanted a->prog to point to the same thing as the argument, then the first line is unnecessary. If you wanted a->prog to copy the thing pointed to by the argument, you need to dereference both pointers, like so: "*a->prog = *prog;"

Also make sure that whenever you use new you also use delete to get rid of the allocated memory, which with your current code you couldn't do because you lose the pointer given to you by new.

Thank you!! I wanted to do the second thing that you said, i didnt notice that!

I change the AVLProg a = new NodeAVLProg; to AVLProg a = new AVLProg; but then i get this error:

cannot convert nodo** to nodo* in initialization.

As i define it, it should be a pointer to a nodo structure... but with AVLProg a = new nodo; doesnt work...

What im trying to do is to implement an AVL tree structure... but i keep having this error when i try to insert an element in the tree and it's in this function :S

Member Avatar for embooglement

Oh, woops. I forgot about that part. Since new returns a pointer, you need to store it in a pointer. Even if you're creating a pointer. So it needs to be "AVLProg * a = new AVLProg;" This make "a" a pointer to an AVLProg, so a pointer to a pointer to a nodo.

Can you made the whole code available in this forum. I am seeing some syntax error in your code, but cannot comment surely if that is the problem without checking full code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.