accessing members inside of a private struct

This is a discussion on accessing members inside of a private struct within the C++ Programming forums, part of the General Programming Boards category; Hey guys. I am trying to access the members inside of my struct, which is inside of my class in ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    accessing members inside of a private struct

    Hey guys. I am trying to access the members inside of my struct, which is inside of my class in the private section like this:

    Code:
    class someClass
    {
        public:
            unsigned getNumber() const { return someStruct.Number; }
    
        private:
    
            struct someStruct
            {
                  unsigned Number;
             };
    };
    This code is in my header file.

    This seems like it should work. I am getting things like this when I compile. warning: no return statement in function returning non-void

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by MyglyMP2 View Post
    This seems like it should work. I am getting things like this when I compile. warning: no return statement in function returning non-void
    The warning has zilch to do with the code you posted.

    And what is it about compiler warnings that turns off people's brains? The warning is telling you EXACTLY what the problem is. Is it that you don't know what "non-void" means, or you don't know what "return" means? Or are you just not thinking?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by MyglyMP2 View Post
    This code is in my header file.
    That seems like a no-no.

    But why not post code that is a minimum compilable snippet that actually demonstrates exactly this issue?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by Dave_Sinkula View Post
    That seems like a no-no.
    It's a simple, const, inline function. I don't see anything wrong with it.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by brewbuck View Post
    It's a simple, const, inline function. I don't see anything wrong with it.
    Generally speaking.

    OP:
    Code:
            unsigned getNumber() const { return someStruct.Number; }
    What is the object's name? (Not the class' name?)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Firstly, I appreciate help, and thank you for replies, but please try to be a little less insulting. Of course I know what return is and non-void. However I don't understand why it has nothing to do with the code that was posted. It is citing that line, and that function has a return type, and is returning something of that same type. I guess I must be missing something...

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    dont you have to declare the struct before the function? just try reversing the order of the private and public parts

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by MyglyMP2 View Post
    Firstly, I appreciate help, and thank you for replies, but please try to be a little less insulting. Of course I know what return is and non-void. However I don't understand why it has nothing to do with the code that was posted. It is citing that line, and that function has a return type, and is returning something of that same type. I guess I must be missing something...
    All too often, when bug hunting, I've found the bug is not where I think it is -- otherwise it really wouldn't be a bug. Quite often the actual bug is in code that I think has nothing to do with where I think it is. A missing header, etc., etc.

    If this offends you, I humbly apologize. I know that code in headers, with several excpetions, is a bad thing to do. Posting all relevant code is a good thing to do. DWYW.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by MyglyMP2 View Post
    I guess I must be missing something...
    someStruct.Number is meaningless.

    EDIT: Sorry for jumping on you, but you didn't SAY that the warning was happening in the code you posted. You just said you were getting some warning. It wouldn't have been the first time a person has posted code that has no relation to the message they are seeing, so I jumped to conclusions. Sincere apologies.

  10. #10
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I think it's because it only got the definition of the struct type, but not the member variable itself. Maybe it should go like this:

    Code:
    class someClass
    {
        public:
            unsigned getNumber() const { return someMember.Number; }
    
        private:
    
            struct someStruct
            {
                  unsigned Number;
             } someMember;
    };
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 03:48 PM

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