Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C, "conflicting types for... " error

Before I continue, here is the code which is giving me an error:

#define numScores 3             // the number of test scores which a student will have

struct btreenode{
int studentID;              // the ID number of the student at the current node

float scores[3];            // the 3 test scores of the student

float average;              // the average of the 3 test scores for the student

struct btreenode *left;     // pointer to left side of the tree
struct btreenode *right;    // pointer to right side of the tree
};

typedef struct btreenode *Node;

I'm getting the following error when I compile:

btreenode.h:17: error: redefinition of 'struct btreenode'
btreenode.h:28: error: conflicting types for 'Node'
btreenode.h:28: note: previous declaration of 'Node' was here

I have a block comment at the top so the line numbers are off, but

line 17 is the first line "struct btreenode{"

line 28 is the last line "typedef struct btreenode *Node"

Does anyone know why i'm getting these errors?

like image 974
jlzizmor Avatar asked Feb 12 '14 04:02

jlzizmor


People also ask

How do you fix undefined reference error in C?

c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.

What does too few arguments mean in C?

Too few arguments to function in C languageThis error occurs when numbers of actual and formal arguments are different in the program.

What is function in C Explain Significance of function with example?

A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.

How do you declare a function in C?

Syntax of Function Declaration int isEvenNumber(int num); Declaration of a function with multiple input parameter and integer return type. int getSum(int num1, int num2); Declaration of a function with no input parameters and integer return type.


2 Answers

The header file should not be included more than once. So use macro in header file to avoid multiple inclusion.

#ifndef TEST_H__
#define TEST_H__

/*you header file can have declarations here*/

#endif /* TEST_H__*/

I am assumed that, this kind of approach is not there in your header file.

like image 115
mahendiran.b Avatar answered Sep 29 '22 05:09

mahendiran.b


It looks as though your btreenode.h file is being included (directly or indirectly) multiple times... that's why the "previous declaration" and the "conflicting types" are in the same file at the same line (previous declaration on the first include, conflicting types when it runs into the same line on the next include).

You should use header guards (in btreenode.h) to prevent the header file code from being processed if it's already been included. At the top of the file, add:

#ifndef BTREENODE_H
#define BTREENODE_H

and at the end of the file add:

#endif // BTREENODE_H

That way, whatever is between those will only be compiled if BTREENODE_H was not already #defined from a previous inclusion.

like image 42
Dmitri Avatar answered Sep 29 '22 05:09

Dmitri