S. N
S. N

Reputation: 3949

Error: conflicting types

I am writing code in C for an assignment.

This is the relevant code:

 //operation: text(elem)
xml_list *text(xml_list *elem){
  if(isEmpty(elem)){
    return Nil();
  }
  return append(text1(head(elem)),text(tail(elem)));
}

//operation: text1(elem)
xml_list *text1(xml_list *elem){
  if(isText(elem)){
    return Cons(elem,Nil());
  }
  else{
    return text(childeren(elem));
  }
}

This gives me the error: error: conflicting types for 'text1' xml_list *text1(xml_list *elem){

I can't figure out the source of the problem. I have been working on this for 8 hours straight now, and I am kind of losing my mind over this. Please some one help me

Upvotes: 0

Views: 274

Answers (2)

user995502
user995502

Reputation:

by the time it reaches

return append(text1(head(elem)),text(tail(elem)));

The compiler doesn't know the return type of text1. So it assumes int and remembers it. And later when it reaches the definition of text1 it will be conflicting.

To solve this forward declare text1 like

xml_list *text1(xml_list *); // <---- Here

xml_list *text(xml_list *elem){
  if(isEmpty(elem)){
    return Nil();
  }
  return append(text1(head(elem)),text(tail(elem)));
}

//operation: text1(elem)
xml_list *text1(xml_list *elem){
  if(isText(elem)){
    return Cons(elem,Nil());
  }
  else{
    return text(childeren(elem));
  }
}

Upvotes: 2

Daniel Fischer
Daniel Fischer

Reputation: 183873

You use text1

xml_list *text(xml_list *elem){
  if(isEmpty(elem)){
    return Nil();
  }
  return append(text1(head(elem)),text(tail(elem)));
}

before it is declared, so the compiler uses (unfortunately) the old "implicit int" rule, and assumes that text1 returns an int.

When the definition that says it returns an xml_list* is encountered, that conflicts with the type obtained from the implicit int declaration.

Declare your functions in a header, and include that in the source, so that all prototypes are known when a function is used.

Upvotes: 3

Related Questions