More Union Struct woes

This is a discussion on More Union Struct woes within the C++ Programming forums, part of the General Programming Boards category; I am trying to clean up my code a bit and utter chaos has overtaken me. I am seriously confused. ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Unhappy More Union Struct woes

    I am trying to clean up my code a bit and utter chaos has overtaken me. I am seriously confused.

    I have an array of 100 structures of 'subsectionData'
    which includes a union of 'sectionSpecificData'
    which includes a struct of 'circleData'
    which includes an array of type double named 'ptX'.

    My question is how in the heck do I assign values to this correctly? I tried several things along these lines and this yielded the fewest number of errors but it looks quite tedious to me,

    subsectionData.sectionSpecificData.circleData.ptX[0] = subsectionData.XBAR;

    and it also gave me these two errors:

    inertia.cpp(353) : error C2274: 'function-style cast' : illegal as right side of '.' operator
    inertia.cpp(353) : error C2228: left of '.ptX' must have class/struct/union type

    Am I even close to getting this correct?





    Code:
    struct sectCircle {
    	double ptX[2];
    	//...other members here.
    } circleData;
    
    
    
    union crossSectionData {
    	struct circleData;
    	//...other members here.
    }sectionSpecificData;
    
    
    
    struct subsection {
    	crossSectionData sectionSpecificData;	
    	//...all kinds of other members here.
    } subsectionData[100];
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    I sure hate it when I ask stupid questions! Thanks for pointing that out to me Salem.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Okay, I fixed my rather obvious error you pointed out.

    subsectionData[0].sectionSpecificData.circleData.ptX[0] = subsectionData[0].XBAR;

    but I get the following two errors:
    : error C2274: 'function-style cast' : illegal as right side of '.' operator
    : error C2228: left of '.ptX' must have class/struct/union type

    So I searched the MS site and came up with this .




    After reading this I tried the following:

    subsectionData[0].sectionSpecificData.circleData.ptX[0] = subsectionData[0].operator double (XBAR);

    Moments of Inertia.cpp
    .cpp(353) : error C2274: 'function-style cast' : illegal as right side of '.' operator
    .cpp(353) : error C2228: left of '.ptX' must have class/struct/union type
    .cpp(354) : error C2039: '.N' : is not a member of 'subsection'
    .cpp(66) : see declaration of 'subsection'
    .cpp(354) : error C2065: 'XBAR' : undeclared identifier
    ...
    Moments of Inertia.exe - 4 error(s), 0 warning(s)

    This obviously didn't correct my problem. Could you please enlighten me as to what I am now misconstruing.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Without full class/struct definitions, we can only guess as to what's wrong. Lucky for you, your compiler aids in this:

    .cpp(354) : error C2039: '.N' : is not a member of 'subsection'
    This simply means that in the class/struct 'subsection', there is no member called 'N'.

    The next line in your errors says "To fix this, go look at your declaration of 'subsection', and you'll see for yourself that there is no member 'N'."

    .cpp(354) : error C2065: 'XBAR' : undeclared identifier
    This means the same thing. It simply means there is no variable/class/etc called 'XBAR'.

    Consider the following example:
    Code:
    #include<iostream>
    int main( void )
    {
        cout << XBAR;
    
        return 0;
    }
    See? My program has nothing in it called 'XBAR', so how can I hope to print it?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Okay now I am absolutely at a loss.

    Code:
    struct sectCircle {
    	double diameter;
    	double ptX[2];
    	double ptY[2];
    } circleData;
    
    
    struct sectRectangle {
    	double ptX[2];	
    	double ptY[2];
    } rectangleData;
    
    
    struct sectTriangle {
    	double ptX[3];	
    	double ptY[3];
    } triangleData;
    
    
    
    union crossSectionData {
    	struct circleData;
    	struct rectangleData;
    	struct triangleData;
    }sectionSpecificData;
    
    
    
    
    struct subsection {
    	int sectionType;
    	crossSectionData sectionSpecificData;	// circle, rectangle, triangle, parabola, sector, segment, ect...
    	double area;
    	double XBAR;
    	double YBAR;
    	double Ixc;
    	double Iyc;
    	double Ixo;
    	double Iyo;
    	double Ixy;
    	double k;
    	double ptX[250];	// Need to get rid of these and replace with cross section
    	double ptY[250];	// specific data union stuctures.
    } subsectionData[100];
    XBAR is a member of subsectionData but for some reason when I added ".operater double(XBAR) " now my compiler says it isn't. And I have no idea where the member 'n' came from because it is not in my code anywhere. The line it refers to with the member 'n' supposedly in it is:
    subsectionData[0].sectionSpecificData.circleData.ptX[0] = subsectionData[0].operator double(XBAR);
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union crossSectionData {
    	struct circleData;
    	struct rectangleData;
    	struct triangleData;
    }sectionSpecificData;
    None of these actually have a variable name. You need:

    Code:
    union crossSectionData {
    	struct circleData aCircle;
    	struct rectangleData aRectangle;
    	struct triangleData aTriangle;
    }sectionSpecificData;
    See? Then replace the word 'circleData' with 'aCircle' in your assignment.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thank you Quzah I will tinker with my code some more. Thanks for the pointer.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bajanine
    Thank you Quzah I will tinker with my code some more. Thanks for the pointer.
    A union is just like a structure in the way that you have to set it up. You provide the variable type, followed by a varaible name. You didn't give any names to your variables, just the variable type. Therefore, there was nothing for you to access.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thank you for your patience Quzah.

    So are you telling me that in this example that circleData is NOT a variable name?
    Code:
    struct sectCircle {
    	double diameter;
    	double ptX[2];
    	double ptY[2];
    } circleData;

    I would have thought I should've done it like so:
    Code:
    union crossSectionData {
    	//type tag-name variable;
    	struct sectCircle circleData;
    	...
    }sectionSpecificData;
    I thought circleData was a variable name and it created an actual instance of my new data type 'sectCircle'. I must be missing a very basic concept here!

    I actually though I had a pretty good handle on structures until this problem cropped up.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bajanine
    Thank you for your patience Quzah.

    So are you telling me that in this example that circleData is NOT a variable name?
    Code:
    struct sectCircle {
    	double diameter;
    	double ptX[2];
    	double ptY[2];
    } circleData;

    I would have thought I should've done it like so:
    Code:
    union crossSectionData {
    	//type tag-name variable;
    	struct sectCircle circleData;
    	...
    }sectionSpecificData;
    I thought circleData was a variable name and it created an actual instance of my new data type 'sectCircle'. I must be missing a very basic concept here!

    I actually though I had a pretty good handle on structures until this problem cropped up.
    All of this is correct. However, look at what you pasted in earlier. Compare:
    Code:
    union crossSectionData {
    	struct circleData;
    	struct rectangleData;
    	struct triangleData;
    }sectionSpecificData;
    See? You have the struct, the name of the structure, but no instance as far as C/C++ is concerned. That's why it wasn't working. It (the language) sees 'circleData' as the type of structure, not as an instance.

    There is 'circleData', but there is no 'sectCircle'. See, you apparently put in a variable name (circleData), but you didn't put in the type. You just put the keyword 'struct'. You didn't put in 'circleSet'.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thanks everybody for you help. I think I have a handle on it now.
    I had finally figured it out. Your final 2 posts help reassure me.

    Thank you very much for holding my hand through this!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 07:32 AM
  4. Class + Union + Struct = Disaster??
    By Inquirer in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:55 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21