"errno_t was not declared in this scope" error

Nov 16, 2014 at 2:27am
Greetings all !
I have a piece of sample code from an open library. It was supposedly written for msvs, but I am using codeblocks with mingw compiler on a windows 7 machine.

The (so I think) relevant piece of code is:

1
2
3
4
5
#include <iostream>
#include <errno.h>
#include <stdio.h>

errno_t err;


The error I get is :
error: 'errno_t' was not declared in this scope|

I searched online for what to do with errno_t, but all I figured out is about its purpose. What can I do to fix this and make the program work ? Thank you.
Kind regards, T
Nov 16, 2014 at 2:30am
http://www.cplusplus.com/reference/cerrno/errno/
http://en.cppreference.com/w/cpp/header/cerrno
I don't think errno_t exists. Instead, you should try decltype(errno)
Nov 16, 2014 at 2:50am
Thank you for reply. But I would like to ask for further assistance.

I wrote this:

typedef decltype(errno) err;

and at the line with code :
err = fopen(&infile, infilename, "r");

I get error:
error: expected unqualified-id before '=' token|

What am I missing and not doing right ?
Thank you, T
Nov 16, 2014 at 3:02am
By using the typedef keyword, you make err a type instead of a variable.

However, in this case since you immediately assign to the variable, you can forget about the data type and let the compiler figure it out for you: auto err = fopen(&infile, infilename, "r");

Though, really, if you are going to use C++ at all, you should use it completely and ignore C:
1
2
3
4
5
6
7
8
if(std::ifstream infile {infilename})
{
    //success
}
else
{
    //error
}
There's generally no use for detailed error information except in specialized cases. For the most part, your only concern is whether you could open the file or not.
Last edited on Nov 16, 2014 at 3:02am
Topic archived. No new replies allowed.