Sign in to follow this  
Red Ant

Defiant Visual Studio - Nested type declarations

Recommended Posts

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???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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: )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this