error C2678
-
This works but is not really correct.
sc->flags = FILE_FLAGS(sc->flags | flag);
I suspect the issue is that enums are usually used to signify single values, i.e the flags field is either FILE_READONLY or FILE_SYSTEM, but not both. If you need to combine values then you should really use a set of
#define
statements. -
Thank you Richard, that error has vanished, but I cannot try this code yet ... why do you say that this solution is not really correct ? The safe solution would be to overload |= operator, but I cannot do that into
enum
, right ?_Flaviu wrote:
why do you say that this solution is not really correct ?
Because, as I mentioned before, enums are used to indicate a set of possible values for an item. For example a class of type Car could contain an enum that indicates the manufacturer, such that the enum definition is:
enum manufacturer
{
AUDI = 1,
BMW,
DAIMLER,
MERCEDES,
// etc.
}And you would never combine thos values in the car type variable. So, in your case you should use
#define
statements, which the compiler will be happy with. -
_Flaviu wrote:
why do you say that this solution is not really correct ?
Because, as I mentioned before, enums are used to indicate a set of possible values for an item. For example a class of type Car could contain an enum that indicates the manufacturer, such that the enum definition is:
enum manufacturer
{
AUDI = 1,
BMW,
DAIMLER,
MERCEDES,
// etc.
}And you would never combine thos values in the car type variable. So, in your case you should use
#define
statements, which the compiler will be happy with. -
But this
enum
is not a part of any struct or class, is just defined into a header file, along with other severalenum
's ... and this enum has a lot of values, that is why I intend to avoid to translated as #define's ... I don't know what to do ...But the issue is, as I keep repeating, that this is not the correct way to use
enum
s. If you want to combine flag values then use#define
, if you want to select one value from a set then useenum
. That is the rule for C and C++, and that is why the compiler complains about your code. -
But the issue is, as I keep repeating, that this is not the correct way to use
enum
s. If you want to combine flag values then use#define
, if you want to select one value from a set then useenum
. That is the rule for C and C++, and that is why the compiler complains about your code. -
Thank you Richard for your patience. BTW, is a legacy code, is not written by me. There will be more suitable to chnage this
enum
to astruct
and where to overload |= operator ? -
Why would you change it to a struct, it only requires a single integer variable to hold all the flags.
int flags = FILE_READONLY | FILE_HIDDEN;
flags |= FILE_SYSTEM; -
Why would you change it to a struct, it only requires a single integer variable to hold all the flags.
int flags = FILE_READONLY | FILE_HIDDEN;
flags |= FILE_SYSTEM;I am afraid that I cannot change that
enum
into #defines, because I have:na->flags |= flag;
where
FILE_FLAGS flag;
and na is struct level1, and inside this level1 struct, I have
struct level1
{
FILE_FLAGS flags;
....
};I have entered in some deadlock ? Please help me to solve this error ...
-
I am afraid that I cannot change that
enum
into #defines, because I have:na->flags |= flag;
where
FILE_FLAGS flag;
and na is struct level1, and inside this level1 struct, I have
struct level1
{
FILE_FLAGS flags;
....
};I have entered in some deadlock ? Please help me to solve this error ...
Try to declare both flags and flag as int
-
Try to declare both flags and flag as int
-
Then, as Richard already suggested replace this enum with a set of #define.
-
Then, as Richard already suggested replace this enum with a set of #define.
-
I am afraid that I cannot change that
enum
into #defines, because I have:na->flags |= flag;
where
FILE_FLAGS flag;
and na is struct level1, and inside this level1 struct, I have
struct level1
{
FILE_FLAGS flags;
....
};I have entered in some deadlock ? Please help me to solve this error ...
-
Of course you can change it to #defines. The definition of
FILE_FLAGS
then becomes anint
type. And then the rest of the code should compile correctly.I am not get something, so forgive me. I have moved those
enum
values as#defines
, but of course, now I get error fromstruct
: doesn't know who isFILE_FLAGS flags;
fromlevel1
struct./*
typedef enum {
FILE_READONLY = 0x00000001,
FILE_HIDDEN = 0x00000002,
FILE_SYSTEM = 0x00000004,FILE\_DIRECTORY = 0x00000010, FILE\_ARCHIVE = 0x00000020, FILE\_DEVICE = 0x00000040, FILE\_NORMAL = 0x00000080, FILE\_TEMPORARY = 0x00000100,
....
.....}FILE_FLAGS;
*/#define FILE_READONLY = 0x00000001
#define FILE_HIDDEN = 0x00000002
#define FILE_SYSTEM = 0x00000004of course, FILE_FLAGS enum has disappeared ... obviously, I don't understood something ... but what ?
struct level1
{
FILE_FLAGS flags; // <-- error
....
}; -
I am not get something, so forgive me. I have moved those
enum
values as#defines
, but of course, now I get error fromstruct
: doesn't know who isFILE_FLAGS flags;
fromlevel1
struct./*
typedef enum {
FILE_READONLY = 0x00000001,
FILE_HIDDEN = 0x00000002,
FILE_SYSTEM = 0x00000004,FILE\_DIRECTORY = 0x00000010, FILE\_ARCHIVE = 0x00000020, FILE\_DEVICE = 0x00000040, FILE\_NORMAL = 0x00000080, FILE\_TEMPORARY = 0x00000100,
....
.....}FILE_FLAGS;
*/#define FILE_READONLY = 0x00000001
#define FILE_HIDDEN = 0x00000002
#define FILE_SYSTEM = 0x00000004of course, FILE_FLAGS enum has disappeared ... obviously, I don't understood something ... but what ?
struct level1
{
FILE_FLAGS flags; // <-- error
....
};_Flaviu wrote:
struct level1 { FILE_FLAGS flags; // <-- error .... };
Try
struct level1
{
int flags; // <-- error
....
}; -
_Flaviu wrote:
struct level1 { FILE_FLAGS flags; // <-- error .... };
Try
struct level1
{
int flags; // <-- error
....
}; -
Ok, but even
flag
is comming as function parameter, just like this:static void SomeFunction(level1* na, FILE_FLAGS flag)
{
....
na->flags |= ~flag;
....
}So, I guess I cannot give up FILE_FLAGS
enum
... don't I ?Change this parameter to be
static void SomeFunction(level1* na, int flag)
{ -
I am not get something, so forgive me. I have moved those
enum
values as#defines
, but of course, now I get error fromstruct
: doesn't know who isFILE_FLAGS flags;
fromlevel1
struct./*
typedef enum {
FILE_READONLY = 0x00000001,
FILE_HIDDEN = 0x00000002,
FILE_SYSTEM = 0x00000004,FILE\_DIRECTORY = 0x00000010, FILE\_ARCHIVE = 0x00000020, FILE\_DEVICE = 0x00000040, FILE\_NORMAL = 0x00000080, FILE\_TEMPORARY = 0x00000100,
....
.....}FILE_FLAGS;
*/#define FILE_READONLY = 0x00000001
#define FILE_HIDDEN = 0x00000002
#define FILE_SYSTEM = 0x00000004of course, FILE_FLAGS enum has disappeared ... obviously, I don't understood something ... but what ?
struct level1
{
FILE_FLAGS flags; // <-- error
....
}; -
Add this line somewhere before the first reference to
FILE_FLAGS
:typedef int FILE_FLAGS;
-
I encounter another error if I made these modifications:
data->com = na->flags & ATTR_IS_COMPRESSED; // <-- error C2059: syntax error : '='
(where data->com is
int
) because nowATTR_IS_COMPRESSED
is#define
, not part ofenum FILE_FLAGS
...What is
data->com
?_Flaviu wrote:
because now
ATTR_IS_COMPRESSED
is#define
, not part ofenum FILE_FLAGS
That is not the reason. I can only assume that you have done a partial change and some parts of your code are still incorrect. If you still have the FILE_FLAGS enum then it is going to cause problems. It would probably help if you showed all the code portions that are connected. It is difficult to be certain of the answers when looking at only a single line of code.