mercredi 12 octobre 2022

conflicting declaration of C function With #ifdef __cplusplus

I failed to define overloading functions with error message of error: conflicting declaration of C function if enclosing by #ifdef __cplusplus blocks.

Below is a simple code for an easy view. This piece of code worked fine without #ifdef __cplusplus blocks.

However, my project code does need #ifdef __cplusplus as it involves combination of C and C++ codes.

Command lines after #ifdef __cplusplus block should be C++, why did it fail to define the overloading function? How to fix this problem with presence of #ifdef __cplusplus blocks?

#include <iostream>
using namespace std;

#ifdef __cplusplus
extern "C"
{
#endif

int add(int x)
{
    return x;
}

int add(int x, int y)
{
    return x + y;
}

int main() {
//  cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    int X = add(2);
    int Z = add(8,2);

    cout <<X<<" "<<Z<<endl;
    return 0;
}

#ifdef __cplusplus
}
#endif

Aucun commentaire:

Enregistrer un commentaire