Defiant Visual Studio - Nested type declarations

Started by
8 comments, last by Qw3r7yU10p! 19 years, 2 months ago
Hello, I've a type that is declared within a class DriveControl ...

typedef unsigned short int word;

class DriveControl
{
public:
    struct ControlWord2_bits
    {
        bool _00_Fixed_Frequency_Bit_1 : 1;
        bool _01_Fixed_Frequency_Bit_2 : 1;
        bool _02_Fixed_Frequency_Bit_3 : 1;
        bool _03_Fixed_Frequency_Bit_4 : 1;
        bool _04_Drive_Data_Set_Bit_0  : 1;
        bool _05_Drive_Data_Set_Bit_1  : 1;
        bool _06_Unused_Bit            : 1;
        bool _07_Unused_Bit            : 1;
        bool _08_PID_Enabled           : 1;
        bool _09_DC_Brake_Enabled      : 1;
        bool _10_Unused_Bit            : 1;
        bool _11_Droop_Enabled         : 1;
        bool _12_Torque_Control        : 1;
        bool _13_External_Fault        : 1;
        bool _14_Unused_Bit            : 1;
        bool _15_Command_Data_Set      : 1;
    };

    union ControlWord2
    {
        ControlWord2_bits  _Flags;
        word _nNumeric;
    };

....
};


... and I need a member variable of type ControlWord2 in one of my other classes, like so:

#include "DriveControl.h"

class PZDRequest
{
private:
    DriveControl::ControlWord2 mv_secondControlWord;

...
};


But MS VC6.0 refuses to cooperate. It spits out the following errors: error C2146: syntax error : missing ';' before identifier 'mv_secondControlWord' error C2602: 'DriveControl::ControlWord2' is not a member of a base class of 'PZDRequest' see declaration of 'PZDRequest' error C2501: 'mv_secondControlWord' : missing storage-class or type specifiers What the hell, what am I doing wrong???
Advertisement
try putting typename in front...I've had problems like that before...

#include "DriveControl.h"class PZDRequest{private:    typename DriveControl::ControlWord2 mv_secondControlWord;...};


I dunno if that will solve it, but its worked for me in the past.
moe.ron
Heh, I didn't even know that keyword until now. But anyway, tried it ... doesn't work. :(
Well, if you need to use ControlWord2_bits outside of DriveControl, then maybe you shouldn't declare it within the DriveControl class?
Please, just don't worry about whether this is good or bad style, just tell me if there is a good reason why it shouldn't work. Because nothing I've ever heard or read about C++ says that it's wrong to use a type declared in one class in another.
Okay, I'm starting to go bananas here! I've just tried to use the struct I declared in DriveControl in another class, and MS VC has no objections to that!! In fact I get the feeling that it only doesn't like my sharing unions across classes, but I don't have the faintest clue why.
Next thing I tried is to move the types from class DriveControl out to the global namespace as suggested by rick_appleton. Now MSVC says 'undeclared identifier' anywhere I got a ControlWord2 ... but strangely prepending a '::' remedies the problem! I don't understand this crazy behavior and I have a strong urge to strangle someone at Microsoft!
Quote:Original post by Red Ant
[snip] but strangely prepending a '::' remedies the problem!

Namespace resolution operator!
Quote:
I don't understand this crazy behavior and I have a strong urge to strangle someone at Microsoft!

Solution: Use a compliant C++ compiler! Order Visual C++ .NET 2003 today (the standard ed. is only about 90€), install the free toolkit, have fun with a good compiler.
Quote:Original post by darookie
Namespace resolution operator!


Yeah, but it has always been my impression that if an identifier isn't found in the local namespace the search is automatically extended to include the global namespace! Which doesn't seem to apply in my example.


Quote:
Solution: Use a compliant C++ compiler! Order Visual C++ .NET 2003 today (the standard ed. is only about 90€), install the free toolkit, have fun with a good compiler.


I would love to, but first I have to convince my boss that this is necessary. But I think I might just give it a try.
This code works fine for me:

typedef unsigned short int word;class DriveControl{public:    struct ControlWord2_bits    {        bool Bit_1 : 1;    };    union ControlWord2    {        ControlWord2_bits  _Flags;        word _nNumeric;    };};class PZDRequest{private:    DriveControl::ControlWord2 mv_secondControlWord;};int main(){    PZDRequest request;    return 0;}


I'm using Microsofts vc++7.1 compiler (which is free... not sure why you're not using it).

Are you showing us the full code or have you got namespaces and stuff. It's probably some (un)obvious error that you're overlooking.
By the way, having a variable names _Flags is just asking for trouble.

Names beginning with underscores are reserved for compiler vendors to use in their implementation (in certain ways). You should not really be using them (unless you're a compiler vendor (c: )

This topic is closed to new replies.

Advertisement