在C ++中调用一个类中的非成员函数,方法具有相同的 [英] Calling in C++ a non member function inside a class with a method with the same
问题描述
我有一个名为打开的实例方法的类,需要调用在C中声明的函数,也称为打开。遵循示例:
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;
}
当我尝试编译它(使用GCC)时,得到以下错误:
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)'
我包含了所有需要的C头文件。当我更改方法名称时,例如 open2 ,我没有编译问题。
I included all the required C headers. When I change the name of the method for example open2 I don't have not problems compiling.
我如何解决这个问题。
提前感谢。
How can I solve this problem. Thanks in advance.
推荐答案
调用
fd = ::open(_portName.c_str(), O_RDWR | O_NOCTTY );
之前的双冒号( ::
)函数名称是C ++的范围解析运算符:
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.
这篇关于在C ++中调用一个类中的非成员函数,方法具有相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!