在C调用++中的类中的非成员函数与相同的方法 [英] Calling in C++ a non member function inside a class with a method with the same

查看:179
本文介绍了在C调用++中的类中的非成员函数与相同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类命名实例方法打开并需要调用用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的名称我没有编译没有问题。

我怎样才能解决这个问题。
先谢谢了。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆