add: I tried on the fun
below anyone know what is the error?
struct node {
string info;
node *link;
node() {
info = "string";
link = NULL;
}
};
void fun( node &node) {
if (node.link !=NULL) {
fun (node.link);
}
}
I get an error message when I use it inside of my functions:
invalid initialization of reference of type ‘node&’ from expression of type ‘node*’
What is the proper way to initialize it?
You're trying to pass a pointer instead of an actual object reference. Simply dereference the pointer like this:
void fun( node &node) {
if (node.link !=NULL) {
fun ( *(node.link) );
}
精彩评论