Like Tree1Likes
  • 1 Post By hk_mp5kpdw

'struct class’ has no member named ‘teach’

This is a discussion on 'struct class’ has no member named ‘teach’ within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> struct class { int number; char name[20]; struct jow { int num; char word[10]; }; float marks; }; ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    41

    'struct class’ has no member named ‘teach’

    Code:
    #include<stdio.h>
    struct class
    {
        int number;
        char name[20];
        struct jow
        {
            int num;
            char word[10];
        };
        float marks;
    };
    int main()
    {
        
        struct class student[2]={{111, "rao", 22.50}, {222,"gto", 22.5}};
        struct jow teach={10, "ann"};
        printf("%s\n", student[1].teach.names);
        return 0;
    }
    Why the error ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    struct jow is nested inside struct class, so is not usable outside struct class. Move its definition outside the definition of struct class.

    Note: if your C compiler is actually a C++ compiler, your code will choke on having a "struct class" too, since class is a C++ keyword.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    I need it as a nested one.

    Code:
    #include<stdio.h>
    struct class
    {
        int number;
        char name[20];
        float marks;
        struct jow
        {
            int num;
            char word[10];
        }teach;
        
        
    }student[2];
        
    int main()
    {
        struct class student[2]={{111, "rao", 22.50}, {222,"gto", 22.5}};
        struct jow teach={10, "ann"};
    
        printf("%s\n", student[1].teach.word);
        return 0;
    }
    No result.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    Nested class types are C++ specific. They are not permitted in C.

    Either build your code as C++ (which will mean you need to change "class" to something else) or follow my original suggestion. No amount of wishing will create a valid third option.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > No result.
    You mean that you saw nothing, because you didn't assign anything to student[1].teach before printing it.
    Code:
    $ cat foo.c
    #include<stdio.h>
    struct class
    {
        int number;
        char name[20];
        float marks;
        struct jow
        {
            int num;
            char word[10];
        }teach;
        
        
    }student[2];
        
    int main()
    {
        struct class student[2]={{111, "rao", 22.50}, {222,"gto", 22.5}};
        struct jow teach={10, "ann"};
        student[0].teach = teach;
    
        printf("%s\n", student[0].teach.word);
        printf("%s\n", teach.word);
        return 0;
    }
    $ gcc -Wall -ansi -pedantic foo.c
    $ ./a.out 
    ann
    ann
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    1. Why do you have both a global array named student and a local array as well.
    Code:
    struct class
    {
        ...
    } student[2];
         
    int main()
    {
        struct class student[2]={{111, "rao", 22.50}, {222,"gto", 22.5}};
    Pick one or the other.



    2. Given this:
    Code:
    struct class
    {
        int number;
        char name[20];
        float marks;
        struct jow
        {
            int num;
            char word[10];
        }teach;
    };
    Instead of this:
    Code:
    struct class student[2]={{111, "rao", 22.50}, {222,"gto", 22.5}};
    struct jow teach={10, "ann"};
    If you wish to initialize a nested structure you can do it like this:
    Code:
    struct class student[2]={{111, "rao", 22.50, {5,"bob"}}, {222,"gto", 22.5, {10,"ann"} }};
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: no member named 'whatever'
    By vileoxidation in forum C++ Programming
    Replies: 31
    Last Post: 10-09-2009, 08:12 PM
  2. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. Replies: 1
    Last Post: 04-17-2008, 07:45 PM
  5. Replies: 4
    Last Post: 12-12-2002, 01:32 PM

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