Calling in C++ a non member function inside a class with a method with the same
I have this class with an instance method named open and need to call a function declared in C also called open. Follows a sample:
void SerialPort::open()
{
if(_open)
return;
fd = open (_portName.c_str(), O_RDWR | O_NOCTTY );
_open = true;
}
When I try to compile it (using GCC) I get the following error:
error: no matching function for call to 'SerialPort::open(const char*, int)'
I included all the required C headers. When I change 开发者_JAVA百科the name of the method for example open2 I don't have not problems compiling.
How can I solve this problem. Thanks in advance.
Call
fd = ::open(_portName.c_str(), O_RDWR | O_NOCTTY );
The double colon (::
) before the function name is C++'s scope resolution operator:
If the resolution operator is placed in front of the variable name then the global variable is affected.
Write ::open
instead of open
. The ::
prefix indicates that the name should be taken from the global scope. (Global namespace? I'm not certain about its exact meaning, to be honest...)
add "::" before open (_portName.c_str(), O_RDWR | O_NOCTTY );
Make sure:
1) You are using namespace resolution if calling function and function being called are in different namespaces, including parent namespace
2) If your calling function is defined above function being called declare the function before caller function. eg:
void bar();
void foo()
{
bar();
}
void bar()
{
....
}
精彩评论