c++ - read was not declared 错误信息

标签 c++ c linux compiler-construction compiler-errors

我正在交叉编译一个包(libqmi)。(简单编译就可以了。)

当我试图遵守 c++ 部分时,问题就出现了。 我收到的消息是

“读取未声明”

我知道在 C 的情况下不需要包含它,但是 C++ 呢?

我尝试手动添加 header :fcntl.hunistd.h 也没有任何解决方案。 (编译器找到了它们并包含了,但错误信息仍然存在)

你知道这背后的问题是什么吗? 我不认为这个问题是错误的,因为它是一个已经实现的并且与主机编译器很好。

编辑: 感谢评论。 主机:Linux,x86, 目标:Linux,arm

unistd.h header 没有解决问题: 我也试过type alloc,可能是misalloc。 GobiQMICore.cpp: 在成员函数'virtual std::vector, std::basic_string >> cGobiQMICore::GetAvailableDevices()'中: GobiQMICore.cpp:319:39: 错误:'read' 未在此范围内声明 GobiQMICore.cpp:334:21: 错误:'close' 未在此范围内声明

代码:

/*===========================================================================
METHOD:
   GetAvailableQDLPorts (Public Method)

DESCRIPTION:
   Return the set of available Gobi QDL ports

RETURN VALUE:
   std::vector <sDeviceID>
===========================================================================*/
std::vector <std::string> cGobiQDLCore::GetAvailableQDLPorts()
{
   std::vector <std::string> devices;

   std::string path = "/sys/bus/usb/devices/";

   std::vector <std::string> files;
   DepthSearch( path,
                2,
                "ttyUSB",
                files );

   int fileNum = files.size();
   for (int i = 0; i < fileNum; i++)
   {
      // Example "/sys/bus/usb/devices/8-1/8-1:1.1/ttyUSB0"
      std::string nodePath = files[i];

      int lastSlash = nodePath.find_last_of( "/" );

      // This is what we want to return if everything else matches
      std::string deviceNode = nodePath.substr( lastSlash + 1 );

      // Move down one directory to the interface level
      std::string curPath = nodePath.substr( 0, lastSlash );

      // Read bInterfaceNumber
      int handle = open( (curPath + "/bInterfaceNumber").c_str(), 
                         O_RDONLY );
      if (handle == -1)
      {
         continue;
      }

      char buff[4];
      memset( buff, 0, 4 );

      bool bFound = false;
      int ret = read( handle, buff, 2 );
      if (ret == 2)
      {
         // Interface 1 or 0
         ret = strncmp( buff, "01", 2 );
         if (ret == 0)
         {
            bFound = true;
         }
         ret = strncmp( buff, "00", 2 );
         if (ret == 0)
         {
            bFound = true;
         }

      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Move down one directory to the device level
      curPath = curPath.substr( 0, curPath.find_last_of( "/" ) );

      // Read idVendor
      handle = open( (curPath + "/idVendor").c_str(), O_RDONLY );
      if (handle == -1)
      {
         continue;
      }
      bFound = false;
      ret = read( handle, buff, 4 );
      if (ret == 4)
      {
         ret = strncmp( buff, "05c6", 4 );
         if (ret == 0)
         {
            bFound = true;
         }
      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Read idProduct
      handle = open( (curPath + "/idProduct").c_str(), O_RDONLY );
      if (handle == -1)
      {
         continue;
      }
      bFound = false;
      ret = read( handle, buff, 4 );
      if (ret == 4)
      {
         ret = strncmp( buff, "920c", 4 );
         if (ret == 0)
         {
            bFound = true;
         }
      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Success!
      devices.push_back( deviceNode );
   }

   return devices;
}

T

最佳答案

I know it does not need to be included in case of C, but what about C++?

我认为您应该始终包含您需要的 header 。我猜你的编译器正在为你完成这项工作,如果你在 gcc 中使用“-Wall”参数,你应该会收到警告。

在 Linux 下,要知道您需要包含什么 header ,只需键入 man function。有时您可能会看到 bash 手册页,要打开,您需要指明部分 man 2 read 并且在概要中,您有所需的标题。要获取这些手册页,您还需要在基于 Debian 的发行版上安装 manpages-dev。

为了回答您的问题,我在使用命名空间编写 C++ 程序时也遇到过此类问题。如果您在命名空间内,请尝试像这样调用此函数 ::read(...)

关于c++ - read was not declared 错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18081245/

相关文章:

c++ - 10 个字符字符串到 Arduino 上的 int

linux - 如何进行持续部署 - C++ on AWS

Centos 6.7 上的 Python 2.7.9?

c++ - 代码在 g++ 中运行完美,但在 Xcode 中运行不完美 - 找不到文件

c++ - gcc-4.9 无法编译#include "user defined file"

c - 如何创建由简单字符数组支持的文件描述符?

c - 在 Windows 平台上,C 代码中的临时重定向 stderr 为 null。

c# - Mono 2.8.1 断言 mono_wsq_count (wsq) == 0

c++ - 使用 Qt 和 QNetworkRequest 恢复失败的 HTTP 下载

c++ - 如何比较CMap Key Case Insensitive?