maygnify
maygnify

Reputation: 3

Multiple Inheritance in C

I am stuck with a classical Multiple Inheritance problem in C.

I have created source files Stack.c and Queue.c. Both of them #include a file Node.c (which containing functions to allocate and deallocate memory). Now, I am trying to implement another program in a single file, for which I need to include both Stack.c and Queue.c. I tried to #include both the files, but the compiler is throwing a conflicting type error.

What is the most correct way to do so?

Thanks in advance!!

Upvotes: 0

Views: 352

Answers (3)

Nobbynob Littlun
Nobbynob Littlun

Reputation: 381

Without seeing details of your program, it is difficult to say. However, conflicting type errors can often be fixed simply by rearranging your code or adding function prototypes.

You see, functions need to be described before they are invoked when reading the source file top to bottom. e.g.:

char foo1 ()
{
    char blah = foo2();
    return blah;
}
char foo2 ()
{
    return 'a';
}

You'd get a conflicting type error because when it's inside foo1, it hasn't seen the declaration for foo2 yet. Thus it assumes that whatever foo2 is, it will return an int. But in actuality it returns a char. These two aren't the same, so... a conflicting type error is reported.

You can fix this by having foo2 come first in the source code, or by inserting a function prototype:

char foo2 (); // function prototype
char foo1 ()
{
    char blah = foo2();
    return blah;
}
char foo2 ()
{
    return 'a';
}

You can also get a conflicting type error if you include source files, for the same reason. #include "Node.c" is essentially a copy-paste. It would be a good idea to switch from including Node.c to including Node.h with externalized functions. You can also avoid a lot of problems if you give prefixes to your function names in source files you plan to include, like.... nodeInsert, nodeDelete, nodeCompare, etc.

Upvotes: 0

Andon M. Coleman
Andon M. Coleman

Reputation: 43359

This will happen if you #include source files (.c)... you are supposed to (for the most part) #include headers (.h). Headers generally provide function prototypes, typedefs, macros, etc. but leave out the actual implementation.

The actual implementation of functions, definitions of variables, etc. should happen exactly once per-compilation unit and usually in a .c file.

If you have other code that needs to re-use functions or variables defined in another compilation unit (e.g. Stack.c), you would #include Stack.h which would provide the function prototypes, global variable names, etc. that you might need.

Once you compile all of your compilation units, it is the linker's job to figure out which object file or library a function or variable is defined in. You drastically complicate its job when you #include "X.c" in another compilation unit, because then you wind up with multiple locations for the same thing (symbols, as the linker likes to call them).

In short, use headers and let the linker do its job.


On a related note, this has nothing to do with multiple-inheritance. That is an object-oriented issue, for languages like C++. The proper name for what you are describing is "symbol collision" or "duplicate symbols".

Upvotes: 1

Simon
Simon

Reputation: 10841

Calling this "multiple inheritance" may be confusing because multiple inheritance is an object-oriented programming issue that doesn't arise in C.

It appears to me that your difficulty may be that you are trying to #include executable code (i.e. .c files) instead of linking the .c files and #including header (.h) files that provide declarations for the functions in the .c files.

Upvotes: 7

Related Questions