Thread: 'i' : cannot access private member declared in class 'a'

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    'i' : cannot access private member declared in class 'a'

    Following flags an error "'i' : cannot access private member declared in class 'a'". Help..
    Code:
    #include<iostream.h>
    class a
    {
    private:
    	int i; float j;
    public:
    	void setdata(int a, float b)
    	{
    	i=a;
    	j=b;
    	}
    };
    
    void main()
    {
    	a b;
    	b.setdata(10,11);
    	cout<<b.i;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You named your variable the same name as your class. Just change your variable name in your setData function to something besides a. Additionally, you have been told before; there is no such thing as void main. main is always declared as an int.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably should write getter functions and use them. Hard to say though, since there is virtually no context from the real problem domain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read a text book or tutorial, and find out what private really means.

    It means you can access it ONLY from member functions!

    You can't just randomly access a private member of a class, you have to use a public function to do it for you. The same way you use setdata() to write to those private variables, you need say a getdata to get them back again.

    I see you're still stuck on the void main, and iostream.h issues - are you paying attention?
    Oh, you're from India, it's time you read my sig-link then.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I made the following changes but the error still persists.
    Code:
    #include<iostream.h>
    class a
    {
    private:
    	int i; float j;
    public:
    	void setdata(int ii, float jj)
    	{
    	i=ii;
    	j=jj;
    	}
    };
    
    int main()
    {
    	a b;
    	b.setdata(10,11);
    	cout<<b.i;
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    My bad, I didn't see you trying to get i directly:
    Code:
    #include<iostream.h>
    class a
    {
    private:
    	int i; float j;
    public:
    	void setdata(int ii, float jj)
    	{
    	i=ii;
    	j=jj;
    	}
                int geti(void){
                     return i;
                }
    };
    
    int main()
    {
    	a b;
    	b.setdata(10,11);
    	cout<<b.geti();
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Got it. Thanx man.

    To salem: whats your sig-link.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    If I change <iostream.h> to <iostream>, I have to additionally say std:: before cout...

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by juice View Post
    If I change <iostream.h> to <iostream>, I have to additionally say std:: before cout...
    or you can invoke the std namespace or just the specific one:
    Code:
    using namespace std;
    
    using std::cout;
    
    std::cout
    One of the 3.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    just learned how to penetrate the privacy of variables

    Code:
    #include<iostream.h>
    class a
    {
    private:
    	int i; float j;
    public:
    	void setdata(int i, float j)
    	{
    		this->i=30;
    	}
    	int geti()
    	{
    	return i;
    	}
    };
    
    int main()
    {
    	int *p;a c;
    	p=(int *)&c;
    	*p=100;
    	cout<<c.geti();
    	return 0;
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > just learned how to penetrate the privacy of variables
    You know, you're just wasting everyone's time don't you.
    You're one step ahead of the rest of your class-mates who only copy/paste TurboC answers from the web, don't blow it now.

    The ability to do random stupid stuff will not make you a better programmer.

    Don't be part of the 97% crowd.
    India Graduates Millions, but Too Few Are Fit to Hire - WSJ.com
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    just learned how to penetrate the privacy of variables
    DO NOT DO THAT! You are now relying on the implementation of the class in a way that is far worse than just having all the member variables be public.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by juice View Post
    just learned how to penetrate the privacy of variables
    You are using an offsetof trick. Nothing special and as Laser pointed out is prone to problems.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Hey Salmem if I am wasting ur or anybody's time then why do u even bother looking at my posts. And not just that, u inturn waste minutes of ur precious time writing a reply for my stupid posts. Don't do that!! Please..

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    And I don't know what u meant wen u said "Oh, you're from India, it's time you read my sig-link then", but f*** u if u try to disrespect me or my nation. And I mean it sir.
    Last edited by juice; 09-28-2011 at 01:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 03:57 PM
  3. private class member access
    By DarkMasterBosel in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2008, 11:37 AM
  4. Replies: 2
    Last Post: 02-14-2008, 02:59 PM
  5. Can't Access the private member from base class
    By planet_abhi in forum C# Programming
    Replies: 3
    Last Post: 01-09-2006, 04:30 AM