Issue with setting a protected member

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avinash

    Issue with setting a protected member


    I have a class defined as follows:

    class main {
    ........
    protected:
    another_class *obj;

    public:
    void set_another_cla ss(another_clas s *ptr);

    }

    main::set_anoth er_class(anothe r_class *ptr)
    {
    obj = ptr;
    }

    When I try to compile this, it gives me the following error:
    "'obj' : undeclared identifier
    '=' : cannot convert from 'class another_class *' to 'int'

    Could somebody let me know why I cannot set a protected member from a
    public method?
    And also suggestion for setting the "another_cl ass" pointer on "main".

  • Ivan Vecerina

    #2
    Re: Issue with setting a protected member

    "Avinash" <avinashr@gmail .comwrote in message
    news:1165904281 .182981.178470@ 79g2000cws.goog legroups.com...
    :
    : I have a class defined as follows:
    :
    : class main {
    : .......
    : protected:
    : another_class *obj;
    //NB: should be declared somewhere...
    :
    : public:
    : void set_another_cla ss(another_clas s *ptr);
    :
    : }
    ; //<-- missing here
    :
    void // missing return value
    : main::set_anoth er_class(anothe r_class *ptr)
    : {
    : obj = ptr;
    : }
    :
    : When I try to compile this, it gives me the following error:
    : "'obj' : undeclared identifier
    : '=' : cannot convert from 'class another_class *' to 'int'
    :
    : Could somebody let me know why I cannot set a protected member
    : from a public method?
    This is not the (root) problem, and since you probably edited the
    code posted here, we cannot tell what error was in your original code.

    But the following will compile without problem:

    class another_class;

    class main { //NB: I would reserve 'main' to int main(), really ...
    protected:
    another_class *obj;
    public:
    void set_another_cla ss(another_clas s *ptr);
    };

    void main::set_anoth er_class(anothe r_class *ptr)
    {
    obj = ptr;
    }



    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <http://www.brainbench.com

    Comment

    • Salt_Peter

      #3
      Re: Issue with setting a protected member


      Avinash wrote:
      I have a class defined as follows:
      >
      class main {
      .......
      protected:
      another_class *obj;
      >
      public:
      void set_another_cla ss(another_clas s *ptr);
      >
      }
      >
      main::set_anoth er_class(anothe r_class *ptr)
      void main::set_anoth er_class(anothe r_class *ptr);
      {
      obj = ptr;
      }
      >
      When I try to compile this, it gives me the following error:
      "'obj' : undeclared identifier
      '=' : cannot convert from 'class another_class *' to 'int'
      >
      Could somebody let me know why I cannot set a protected member from a
      public method?
      Thats not your problem. There is an issue with another-class or with an
      overload of its op=.
      Did you forget to #include it?
      And also suggestion for setting the "another_cl ass" pointer on "main".
      Why aren't you using a const reference instead and setting that in
      ctor?
      And why deal with the name clash between int main() and your class
      name? Isn't life complicated enough?
      Always initialize your members. Also, if you are going to use dumb
      pointers, you should at least make the minimum efffort to protect that
      critical pointer or you'll find yourself in hot water real fast since
      anyone could modify that pointer by mistake.

      This works:

      #include <iostream>
      #include <ostream>

      class another_class { };

      template< typename A >
      class Main
      {
      A* p_another;
      public:
      Main() : p_another(0) { } // def ctor with init list
      void set_another_cla ss(A* const ptr);
      A* const getA() const { return p_another; }
      };

      template< typename A >
      void Main< A >::set_another_ class(A* const ptr)
      {
      p_another = ptr;
      }

      int main() {
      Main< another_class m;
      another_class ac;
      m.set_another_c lass( &ac );
      std::cout << "p_another = " << m.getA();
      std::cout << std::endl;

      // another_class* p_A = m.getA(); // error
      another_class* const p_A = m.getA(); // ok, const ptr
      // p_A = 0; // error, read-only
      // m.getA() = 0; // error, invalid l-value
      std::cout << "p_A = " << p_A;
      std::cout << std::endl;
      }

      Comment

      Working...