The solution of “no matching function for call to…” in G + + compilation

If a function with the same name as the library function is defined in the class, such as printf in the following code, when the library function printf is called again, there will be a compilation error similar to “no matching function for call to…”, even if the parameters and return values of the function defined in the class and the library function are not the same

#include < stdio.h>

class CAClass{

public:

CAClass() { printf(“hello\n”); }

~CAClass() {}

private:

void printf() { }

};

int main()

{

CAClass aClass;

return 0;

}

The solution is to add the namespace to the header file containing the library function, and add the namespace when calling, so that the problem can be solved. The code is as follows:

namespace stdclib {
#include < stdio.h>
}

class CAClass{
public:
CAClass() { stdclib::printf(“hello\n”); }
~CAClass() {}
private:
void printf() { }
};

int main()
{
CAClass aClass;
return 0;
}

Similar Posts: