小编Kid*_*ser的帖子

C++中errno变量的类型是什么?

我在C++中进行一些系统编程,我想将linux系统中的errno变量传递给我的异常处理程序.您可以在https://pastebin.com/ppgMc8Hj中查看示例代码

#include <sys/stat.h>
#include <cerrno>
#include <iostream>
#include <cstring>

std::string errorString(int errno){
    return std::strerror(errno);
}

int main(){
    struct stat sb;
    errno = 0;
    if(stat("/jdfj/", &sb)){
        errorString(errno);
    }
    else{
        std::cout << std::boolalpha << S_ISDIR(sb.st_mode) << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

但它会返回这样的错误

In function 'int main()': 
 14:26: error: invalid conversion from 'int' to 'int* (*)()' [-fpermissive] 
  6:13: note: initializing argument 1 of 'std::string errorString(int* (*)())'.
Run Code Online (Sandbox Code Playgroud)

我在这里看到http://en.cppreference.com/w/cpp/string/byte/strerror,从errno返回字符串的标准函数接受一个int作为参数.我的问题是

  1. 如果是int类型,为什么它不能在上面的代码中工作?
  2. 如果不是它的类型?

c++ errno

1
推荐指数
1
解决办法
1019
查看次数

标签 统计

c++ ×1

errno ×1