request for member , which is of non-class type | - error when compiling

This is a discussion on request for member , which is of non-class type | - error when compiling within the C++ Programming forums, part of the General Programming Boards category; Hello all, Noob at c++. Trying to make a friends contact function using a struct that creates a 3D array ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    request for member , which is of non-class type | - error when compiling

    Hello all,


    Noob at c++. Trying to make a friends contact function using a struct that creates a 3D array dynamically with pointers. The code for creating the array appears to be correct.......


    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    struct friends_of_mine { // Create struct.
    
        string name;
        int lcontact;
        int dcontact;
    };
    
    
    
    
    
    
    friends_of_mine contact( int size); // Declare the function.
    
    
    int main ()
    {
        int menu;
        cout << "Welcome to the Contact Manager" << "\n";
        cout << "Please choose from the following options" << "\n";
        cout << "Press 1 - To add contacts" << "\n";
        cout << "Press 2 - To display all contacts" << "\n";
        cout << "Press 3 - To modify the contacts database" << "\n";
        cout << "Press 4 - To quit" << "\n";
        cin >> menu;
    
    
        switch ( menu ) // This switch/case is a work in progress.
    
    
        {
        case 1:
                cout << "How many contacts would you like to add?"<<"\n";
                int size;
                cin >> size;
                contact(size);
                break;
        }
    
    
    }
    friends_of_mine contact (int size)
    {
    
        friends_of_mine ***p_p_p_Contact; 
        // Create a pointer to an array of pointers to pointers.
    
        p_p_p_Contact = new friends_of_mine**[size];
        // Dynamically allocate a new array using friends_of_mine struct
         //type.
    
    
    
    
    
    
        for ( int i = 0; i < size; i++)
        {
            p_p_p_Contact[i] = new friends_of_mine *[size];
            // Now make the array of pointers to pointers, 
             //POINT to a secondary array of pointers.
    
    
                for ( int j = 0; j < size; j++)
                    {
                        p_p_p_Contact[i][j] = new friends_of_mine[size];
                        // Now make that array of pointers point to an array of
                        //structs with type friends_of_mine.
                    }
    
        }

    but when it comes to inputting data to the array, the syntax or semantics used with cin appears to be an issue and the compiler halts here -


    Code:
      
    for ( int i = 0; i < size; i++ )
    
        {
                cout << "Enter the name of your friend" << "\n";
                cin >> p_p_p_Contact[i].name;   
                 // Here is where the compiler complains -
    
      /*friends_3darray.cpp|64|error: request for member 'name' in 
    '*(p_p_p_Contact + ((unsigned int)(((unsigned int)i) * 4u)))', 
    which is of non-class type 'friends_of_mine**'|*/
    
    
                for ( int j = 0; j < size; j++ )
                    {
                        cout << "Enter the date of last contact" << "\n";
                        cin >> p_p_p_Contact[j].lcontact;
    
    
                        for ( int k = 0; k < size; k++ )
                        {
                            cout << "Enter the days since last contact" << "\n";
                            cin >> p_p_p_Contact[k].dcontact;
                        }
                    }
            }
    }

    Any ideas as to what I could be doing wrong?


    thank you in advance!


    spark*

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To access the actual struct members, you need three indices. Something like this:
    Code:
    p_p_p_Contact[i][j][k].name
    However, I have no idea why you have a 3D array in the first place. Can you explain that?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    lol, being the noob that I am, its quite possible that am not applying the most optimal approach to my problem. I am currently studying Alex Allain's "Jumping into C++" and am trying to resolve a practice problem presented in the text.

    Here it is -

    Write a program that lets users keep track of the last time they talked to each of their friends.
    Users should be able to add new friends (as many as they want!) and store the number of days
    ago that they last talked to each friend.

    Let users update this value (but don't let them put in bogus numbers like negative values).
    Make it possible to display the list sorted by the names of the friends of by how recently it
    was since they talked to each friend.

    From here you can see that 3 variables need to be accounted for. One of type string (friend name), and the other two of type int ("number of days since last contact", and "last time they talked to each of their friends"), thus the need for a 3D array? I am gathering from your response that there may be a much better way to resolve this practice problem?

    Thank you so very much for your time!

    spark*
    Last edited by spark*; 07-23-2012 at 11:17 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You want a one-dimensional array of structs here. Each struct will hold the 3 items you want, so that's where the number 3 comes in, not in the number of dimensions.

    Actually, to me it looks like you just need two data items: name and days_since_last_contact.
    Code:
    struct friends_of_mine {
        string name;
        int last_contact;
    };
    If you've studied vectors then that would be a better choice instead of a plain-old array. Any resizing is done automatically with a vector.

    PS, you could also have a "friends_of_ours" for your mob-connected friends.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    oogabooga -

    You want a one-dimensional array of structs here. Each struct will hold the 3 items you want, so that's where the number 3 comes in, not in the number of dimensions.
    Duh, of course!

    Also realized that I had my focus set on the how the problem was explained in the first paragraph, the date of last contact was explained 2 different ways actually.

    Haven't got to vectors yet, gotta study linked lists and recursion first.

    "friends_of_ours" struct to be included in next version

    Thanks for pointing out my errors, You absolutely RULE!

    spark*
    Last edited by spark*; 07-23-2012 at 11:54 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by spark* View Post
    Haven't got to vectors yet, gotta study linked lists and recursion first.
    Sigh. If you are going to work with dynamic arrays, you had better learn how to use vectors first... -_-
    Is that your book that is stupid? Or is this some kind of stupid class?
    In fact, I'd recommend that you jump ahead to vectors right now, learn now, and jump back. If possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-16-2010, 07:27 AM
  2. Replies: 4
    Last Post: 09-04-2010, 10:12 AM
  3. Replies: 1
    Last Post: 05-18-2010, 05:14 AM
  4. Getting a:"member is not of class type" error
    By indigo0086 in forum C++ Programming
    Replies: 10
    Last Post: 11-20-2006, 10:19 AM
  5. error: request for member of non-aggregate type
    By curlious in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2004, 10:36 AM

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