【问题标题】:Associate POSIX::open function to a namespace将 POSIX::open 函数关联到命名空间
【发布时间】:2017-10-24 12:55:26
【问题描述】:

我正在开发一个需要公开controller::open() 函数的控制c++ 库。但是在这个函数中,我必须调用POSIX::open() 函数来打开一个文件描述符。编译器抱怨我向控制器函数发送了无效参数并且不明白我想调用 POSIX open() 文件函数。

这是我的代码:

类声明:

class PosixReadController {  
int open();
}

实现:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int PosixReadController::open()
{
  int fd = open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
  {
   /*
    * Could not open the port.
    */

    perror("open_port: Unable to open /dev/ttyf1 - ");
  }
  else
    fcntl(fd, F_SETFL, 0);

return fd;
}

错误信息(日食):

无效参数:'Candidates are: int open()'

使用::open 更改调用以打开到全局命名空间没有帮助。因为我看到库已经包含 open 函数并且我收到以下错误:

无效参数:'候选者是:int open(const char*, int, ...)
ImageCtrl* 打开(图像)''

有什么想法吗?

【问题讨论】:

  • 您的环境中真的有POSIX 命名空间吗?还是只包含标准的&lt;fcntl.h&gt; POSIX 标头?
  • 哼,把你的问题相关的代码分享一下亲爱的1304怎么样?
  • 很可能只是在全局范围内:::open()
  • 在本站发布67个问题后,您应该知道需要显示相关代码和错误信息。
  • 如果将open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY); 更改为::open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY);,是否会出现错误?如果有,那是什么?

标签: c++ posix


【解决方案1】:

无效参数:'候选人是:int open(char*, int, ...)

这是非常可疑的。你从哪里得到open 的声明?是否包含&lt;fcntl.h&gt;

原型实际上应该是这样的:

int open(const char *, int, ...);

const char * 会匹配您传递的字符串文字,但 char * 显然不匹配,因为字符串文字不可写。

【讨论】:

  • 你是对的,我开发代码时无法从服务器复制,所以我必须手动输入所有内容,我错过了 const。现在已经修好了。
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
相关资源
最近更新 更多