Thread: error: 'int A::mas100 [100]' is private

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    error: 'int A::mas100 [100]' is private

    Good day to all the good!

    Here's a seemingly error in this code, all right. I don't understand why there is an error. Tell me, please!

    Code:
    class A
    {
        private:
        int mas100[100]; //error: 'int A::mas100 [100]' is private
        public:
        int& operator[](int n)
        {
            //if(n < 0 || n >= 100) exit(-1);
            try
                  {
                      if(n < 0 || n >= 100)
                      {
                          throw "Error!!!!"; //створити символьний рядок
                      }
    
                  }
                  catch(char *str)//сюди передасться рядок
                  {
                      printf(str);
    
                  }
    
            return mas100[n];
        }
    };
    Code:
    A a1;
        for(int i=0; i<100; i++)
        {
            a1.mas100[i]=i;  //error: within this context
        }
        for(int j = 0; j < 100; j++)
        {
            int temp = a1[j]; // використовуємо функцію праворуч від знака =
        }

    ||=== Build: Debug in Mistructur1 (compiler: GNU GCC Compiler) ===|
    D:\Classes\Mistructur1\Mistructur1.cpp||In function 'int main()':|
    D:\Classes\Mistructur1\Mistructur1.cpp|81|error: 'int A::mas100 [100]' is private|
    D:\Classes\Mistructur1\Mistructur1.cpp|122|error: within this context|
    D:\Classes\Mistructur1\Mistructur1.cpp|126|warning : unused variable 'temp' [-Wunused-variable]|
    ||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    Last edited by Dmy; 07-24-2017 at 05:13 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Do you know what "private" means?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "private", as Elkvis hinted, is important here. You can't use a private member of an object directly, that is its purpose in programming, to hide the underlying structure. If you want to get or set its value, you need appropriate public methods to go with it.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you very much! I appreciate your answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird compilation error in call of private function!
    By MutantJohn in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2016, 03:23 PM
  2. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  3. Replies: 2
    Last Post: 02-14-2008, 02:59 PM
  4. Replies: 9
    Last Post: 05-03-2007, 03:50 PM
  5. Why Private
    By Manitoadlet in forum C++ Programming
    Replies: 8
    Last Post: 09-16-2002, 08:18 AM

Tags for this Thread