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:
- C write and read file via FILE and open method in a+ or r mode
- C++ 11: How to Avoid Deadlock in unique_Lock and lock_Guard
- Makefile:xxx: recipe for target ‘xxx’ failed
- C Compilation Error: implicit declaration of function xxx is invalid in C99 [-Wimplicit-function-declaration]
- PHP Fatal error: Cannot redeclare class
- Using SQLite to show undefined reference to ` SQLite3_ open’…
- C++ Error: passing ” “as” ” discards qualifiers
- invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’
- [Solved] Run-Time Check Failure #2 – Stack around the variable ‘a’ was corrupted
- error LNK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MDd_DynamicDebug’ does not match value ‘MTd_StaticDebug’