calling structures within structures

This is a discussion on calling structures within structures within the C++ Programming forums, part of the General Programming Boards category; Hi again. I've got this program that's supposed to generate Christmas cards. it does this using hard coded information and ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    calling structures within structures

    Hi again.

    I've got this program that's supposed to generate Christmas cards. it does this using hard coded information and information given by the user.

    it uses the following stucture at its base;

    Code:
    struct Basecard
    {
    	struct persons //structure that holds the names
    	{
    	    char sender[name]; //senders name
            char reciptfore[name]; //recipients forename
            char reciptsur[name]; //recipients surname
    	};
    	struct messages //structure tha
    	{
    	    char standard[message];
    	    char personal[message];
    	};
    	struct Images //structure that contains the images of the built card
    		{
    			Image main_image;
    			Image image1;
    			Image image2;
    			Image image3;
    			Image image4;
    the problems occur when I'm trying to use the structures. here's an example

    Code:
    	cout << "please enter the name of the sender: ";
    	cin >> card.persons.sender; endl;
    gets the compiler error invalid use of struct Basecard:: persons

    I'm guess i'm calling it wrong. can anyone tell me the proper way?

    thanks

    ES

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elsparko View Post
    I'm guess i'm calling it wrong. can anyone tell me the proper way?
    Yeah, "persons" is not an instance, it's (part of) the name of the datatype. Try something like this:
    Code:
    struct Basecard
    {
    	struct _persons //structure that holds the names
    	{
    		char sender[name]; //senders name
    		char reciptfore[name]; //recipients forename
    		char reciptsur[name]; //recipients surname
    	} persons;
    Now "persons" is an instance of a "struct _persons" it will work. You can actually take "_persons" right out and use an anonymous struct there.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    persons is a type, not a variable. You need a variable of that type to use, in the same way that you are not doing Basecard.persons.char in your code.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Ok, i've changed the code to what I think you wanted me too, the structure now looks like this.

    Code:
     struct Basecard
    {
    	struct _persons //structure that holds the names
    	{
    	    char sender[name]; //senders name
            char reciptfore[name]; //recipients forename
            char reciptsur[name]; //recipients surname
    	};_persons persons;
    	struct _messages //structure tha
    	{
    	    char standard[message];
    	    char personal[message];
    	};_messages messages;
    	struct _Images //structure that contains the images of the built card
    		{
    			Image main_image;
    			Image image1;
    			Image image2;
    			Image image3;
    			Image image4;
    		};_Images Images;
    };
    and i'm no longer getting the invalid use error, but now i'm getting some different ones :S

    Code:
    cout << "please enter the name of the sender: ";
    	cin >> card.persons.sender; endl;
    gives the error statement cannot resolve address of overloaded function. to the best of my knowleadge, I am not overloading this function as it is not used anywhere othre than here.

    Code:
    message_ = "Merry Christmas";
    gives the error incompatible types in assignment of 'const char [16]' to 'char[51]'. I assumed it had something to do with the variable type, but whatever I change it too only seems to make it angrier.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    endl is not a function. Don't try to use it as one. There's no reason "endl" and "cin" should ever appear on the same line anyway.

    You can't assign to char arrays, just like you can't assign to integer arrays or float arrays. If you insist on using old C-style strings, then you'll have to use strcpy just like you would in C.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    Quote Originally Posted by tabstop
    endl is not a function. Don't try to use it as one. There's no reason "endl" and "cin" should ever appear on the same line anyway.
    Actually, std::endl is a function template, but being an output stream manipulator, you indeed should not be trying to use it with an input stream like std::cin.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 12:24 AM
  2. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  3. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 02:12 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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