Thread: Member is private..??

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Member is private..??

    I thought member functions of a class are supposed to be able to access all members of that class, regardless of whether those said members are private or not?
    Why then when I try to return a value from a member function of a class which returns a value of a private member of that class, my compiler complains its private??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you sure you're returning the value?

    And not a reference or a pointer to it?
    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.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Here's the code:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class CanEnumToStrings {
    
      public:
        CanEnumToStrings();
       vector <string>* getEnumValues();
       string getNameOfEnum();
       int getNumOfEnumValues();
    
        enum anEnum {
          anEnumValue1,
          anEnumValue2,
          anEnumValue3
        };
    
       string getEnumValue(anEnum enumValue);
    
      private:
        vector <string> enumValueNames;
        string nameOfEnum;
        int numOfEnumValues;
    
    };
    
    CanEnumToStrings::CanEnumToStrings() {
    
      nameOfEnum = "anEnum"; //initialize to name of enum
      numOfEnumValues = 3; //initialize to number of enum values
      enumValueNames.push_back("anEnumValue1"); //add string name of enum value to vector of enum values
      enumValueNames.push_back("anEnumValue2"); //add string name of enum value to vector of enum values
      enumValueNames.push_back("anEnumValue3"); //add string name of enum value to vector of enum values
    
    }
    
    vector <string>* CanEnumToStrings::getEnumValues() {
    
      vector <string>* pEnumValueNames = &enumValueNames; //get a pointer to the enum value names
      return pEnumValueNames; //return the pointer to the vector of enum value names
    
    }
    
    string CanEnumToStrings::getNameOfEnum() {
    
        return nameOfEnum;
    
    }
    
    int CanEnumToStrings::getNumOfEnumValues() {
    
        return numOfEnumValues;
    
    }
    
    string CanEnumToStrings::getEnumValue(anEnum enumValue) {
    
      if (enumValue == anEnumValue1) {
          return "anEnumValue1";
      }
    
      if (enumValue == anEnumValue2) {
          return "anEnumValue2";
      }
    
      if (enumValue == anEnumValue3) {
          return "anEnumValue3";
      }
    
    }
    
    int main() {
    
        CanEnumToStrings object; //create an object of the "CanEnumToStrings" class
        vector <string>* enumValuesVector = object.getEnumValues();
        int enumValuesVectorSize = enumValuesVector->size();
        string nameOfEnum = object.getNameOfEnum();
        int numOfEnumValues = object.numOfEnumValues();
    
        cout<< "Thank you for running this program.\n"
               "The name of the enum is: " << nameOfEnum <<endl;
        cout<< "The number of enum values in the enum is: " << numOfEnumValues <<endl;
        for (int i = 0; i < enumValuesVectorSize; i++) {
            cout<< "The current enum value is: " << enumValuesVector->at(i) <<endl;
        }
    
        cout<< "\n\nPress Enter to end this program." <<endl;
        cin.get();
    
        return 0;
    
    }

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    Are you sure you're returning the value?

    And not a reference or a pointer to it?
    Positive. I'm getting the return value of the member function and storing it in an int in the main() function.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Programmer_P View Post
    Here's the code:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class CanEnumToStrings {
    
      public:
        CanEnumToStrings();
       vector <string>* getEnumValues();
       string getNameOfEnum();
       int getNumOfEnumValues();
    
        enum anEnum {
          anEnumValue1,
          anEnumValue2,
          anEnumValue3
        };
    
       string getEnumValue(anEnum enumValue);
    
      private:
        vector <string> enumValueNames;
        string nameOfEnum;
        int numOfEnumValues;
    
    };
    
    CanEnumToStrings::CanEnumToStrings() {
    
      nameOfEnum = "anEnum"; //initialize to name of enum
      numOfEnumValues = 3; //initialize to number of enum values
      enumValueNames.push_back("anEnumValue1"); //add string name of enum value to vector of enum values
      enumValueNames.push_back("anEnumValue2"); //add string name of enum value to vector of enum values
      enumValueNames.push_back("anEnumValue3"); //add string name of enum value to vector of enum values
    
    }
    
    vector <string>* CanEnumToStrings::getEnumValues() {
    
      vector <string>* pEnumValueNames = &enumValueNames; //get a pointer to the enum value names
      return pEnumValueNames; //return the pointer to the vector of enum value names
    
    }
    
    string CanEnumToStrings::getNameOfEnum() {
    
        return nameOfEnum;
    
    }
    
    int CanEnumToStrings::getNumOfEnumValues() {
    
        return numOfEnumValues;
    
    }
    
    string CanEnumToStrings::getEnumValue(anEnum enumValue) {
    
      if (enumValue == anEnumValue1) {
          return "anEnumValue1";
      }
    
      if (enumValue == anEnumValue2) {
          return "anEnumValue2";
      }
    
      if (enumValue == anEnumValue3) {
          return "anEnumValue3";
      }
    
    }
    
    int main() {
    
        CanEnumToStrings object; //create an object of the "CanEnumToStrings" class
        vector <string>* enumValuesVector = object.getEnumValues();
        int enumValuesVectorSize = enumValuesVector->size();
        string nameOfEnum = object.getNameOfEnum();
        int numOfEnumValues = object.numOfEnumValues();
    
        cout<< "Thank you for running this program.\n"
               "The name of the enum is: " << nameOfEnum <<endl;
        cout<< "The number of enum values in the enum is: " << numOfEnumValues <<endl;
        for (int i = 0; i < enumValuesVectorSize; i++) {
            cout<< "The current enum value is: " << enumValuesVector->at(i) <<endl;
        }
    
        cout<< "\n\nPress Enter to end this program." <<endl;
        cin.get();
    
        return 0;
    
    }
    object.numOfEnumValues (in red, see quote) is the private member variable which you are trying to access from outside. It's not even a function...
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > return pEnumValueNames; //return the pointer to the vector of enum value names
    What is this, if it ISN'T a pointer to a private member?
    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.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Oh yeah...oops.
    I accidentally wrote the wrong thing.

    I meant to do:

    Code:
    int numOfEnumValues = object.getNumOfEnumValues();
    Thanks.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, that about wraps it up...
    My program's done now.

    I'm done asking stupid questions.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by Salem
    > return pEnumValueNames; //return the pointer to the vector of enum value names
    What is this, if it ISN'T a pointer to a private member?
    Why did you delete your post?
    Yes, its a pointer to a private member. Is that a problem?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also see multiple problems in your code. First off, getters should be const. They should return something but not allow that something to be modified. That's a setter. You're violating this definition.
    Furthermore, you return a pointer to a member. Why not just return a (const) reference?
    I also see that you basically have an if/else if clause to translate "strings" into their integer equivalents. I would suggest you use a map here. std::map should suffice.
    You also seem to create an initialize a number of unnecessary variables, such as name and number of enum constants. You're never modifying these. You can just treat these as constants and return them directly in your code, or if you will, make them const and initialize them in the initialize list.
    But I think this whole class is a shame. Basically it's just an inefficient map.
    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.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's another common approach to enum-string conversions which is fairly easy to maintain:

    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    
    class foo
    {
    	public:
    	
    	enum id
    	{
    		zero, 
    		one, 
    		two, 
    		three, 
    		four, 
    	/*
    		Remember: add additional enums before this one!
    	*/	
    		enum_count
    	};
    	
    	std::string enum_to_string( id index ) const
    	{
    		if( index >= enum_count )
    			return "<<undefined>>";
    		static std::string
    			names[ enum_count ] = 
    		{
    			"zero", 
    			"one", 
    			"two", 
    			"three", 
    			"four"			
    		};
    		return names[ index ];
    	}
    	
    	std::vector< std::string > get_enum_strings( void ) const
    	{
    		std::vector< std::string >
    			result;
    		for( size_t index = 0; index < enum_count; ++index )
    			result.push_back( enum_to_string( id( index ) ) );
    		return result;
    	}	
    };
    
    int main( void )
    {
    	using namespace 
    		std;
    	foo
    		f;
    	cout << f.enum_to_string( foo::four ) << endl;
    	cout << f.enum_to_string( foo::id( 4 ) ) << endl;
    	cout << f.enum_to_string( foo::id( 7 ) ) << endl;
    	vector< string >
    		v = f.get_enum_strings( );
    	copy( v.begin( ), v.end( ), ostream_iterator< string >( cout, "\n" ) );
    }
    Hth.
    Last edited by Sebastiani; 06-03-2010 at 03:11 PM. Reason: added 'static' qualifier to 'names' variable
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Sebastiani View Post
    Here's another common approach to enum-string conversions which is fairly easy to maintain:

    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    
    class foo
    {
    	public:
    	
    	enum id
    	{
    		zero, 
    		one, 
    		two, 
    		three, 
    		four, 
    	/*
    		Remember: add additional enums before this one!
    	*/	
    		enum_count
    	};
    	
    	std::string enum_to_string( id index ) const
    	{
    		if( index >= enum_count )
    			return "<<undefined>>";
    		static std::string
    			names[ enum_count ] = 
    		{
    			"zero", 
    			"one", 
    			"two", 
    			"three", 
    			"four"			
    		};
    		return names[ index ];
    	}
    	
    	std::vector< std::string > get_enum_strings( void ) const
    	{
    		std::vector< std::string >
    			result;
    		for( size_t index = 0; index < enum_count; ++index )
    			result.push_back( enum_to_string( id( index ) ) );
    		return result;
    	}	
    };
    
    int main( void )
    {
    	using namespace 
    		std;
    	foo
    		f;
    	cout << f.enum_to_string( foo::four ) << endl;
    	cout << f.enum_to_string( foo::id( 4 ) ) << endl;
    	cout << f.enum_to_string( foo::id( 7 ) ) << endl;
    	vector< string >
    		v = f.get_enum_strings( );
    	copy( v.begin( ), v.end( ), ostream_iterator< string >( cout, "\n" ) );
    }
    Hth.
    The problem with that approach (and other similiar approaches) is, its not generic enough to work with ANY enum. Not to mention, enums can often be quite long, and the thought of hard-coding each one just to get the string names of the enum values doesn't seem like the best idea to me. That is why I wrote a program to handle this, and it works nice. I'll post it here in a minute.
    Last edited by Programmer_P; 06-03-2010 at 05:17 PM.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    How about a macro then?
    Code:
    #include <iostream>
    
    #define WYSIWYG(m) (#m)
    
    int main ()
    {
        enum { Foo , Bar , Quz };
    
        std::cout << WYSIWYG(Foo) << '\n';
        std::cout << WYSIWYG(Bar) << '\n';
        std::cout << WYSIWYG(Quz) << '\n';
    
        return 0;
    }
    //my output
    
    //Foo
    //Bar
    //Quz
    //
    //Process returned 0 (0x0)   execution time : 0.031 s
    //Press any key to continue.
    I know it isn't some fabulous C++ thing ...

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    How about a macro then?
    Code:
    #include <iostream>
    
    #define WYSIWYG(m) (#m)
    
    int main ()
    {
        enum { Foo , Bar , Quz };
    
        std::cout << WYSIWYG(Foo) << '\n';
        std::cout << WYSIWYG(Bar) << '\n';
        std::cout << WYSIWYG(Quz) << '\n';
    
        return 0;
    }
    //my output
    
    //Foo
    //Bar
    //Quz
    //
    //Process returned 0 (0x0)   execution time : 0.031 s
    //Press any key to continue.
    I know it isn't some fabulous C++ thing ...
    That is better, but once again, you still have to pass the name of the enum value to the macro to get back the string value, which means you're still hard-coding it.
    My program gets an enum from file, writes it to a header (or source, if you so choose...) in a class, and provides a couple of member functions. One will retrieve a vector of strings containing all the enum values converted to strings, and the other will retrieve a specific enum value if you want to do it that way.
    I also provide a method for getting the name of the enum, and another method for getting the number of enum values.
    That way, say you have an enum you wish to get all the enum values converted to strings of, you run my program, pass to it the filepath to the source or header file which contains the enum. My program will then generate a file which you can include in your program, and you can then call a simple function to retrieve the vector of string names of enum values, to do whatever you need to after that.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What? But enums are only hard-coded enumerated constants. If you make it more than that, you're doing something totally different. If you want to use the constant, do that; if you want to save the stringified name, it's not hard to use a string array or whatever.
    Last edited by whiteflags; 06-03-2010 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM