Widoczność metod w klasie zagnieżdżonej

0

Witam.
Chcę w klasie b odpalić metodę1 niestety brak jej widoczności.

Kompilator gcc version 4.4.4 (GCC)
Configured with: ../gcc-4.4.4/configure --prefix=/usr --libdir=/usr/lib64 --enable-shared --enable-bootstrap --enable-languages=c,c++,ada,fortran,java,objc --enable-threads=posix --enable-checking=release --with-system-zlib --with-python-dir=/lib64/python2.6/site-packages --disable-libunwind-exceptions --enable-__cxa_atexit --enable-libssp --with-gnu-ld --verbose --enable-multilib --target=x86_64-slackware-linux --build=x86_64-slackware-linux --host=x86_64-slackware-linux
Thread model: posix

Pozdrawiam

class Test {
public:
int var1;
Test(){
var1 = 10;
};
~Test();

class b {
    public:
        void metoda2(){
            Test->var1=11;
            Test->metoda1();
        };
};

void metoda1(){
    cout << var1 << endl;
};

};

0

Jeżeli przy tej klasie napiszesz:

   Test x(10),y(20),z(30);
   Test::b().metoda2();

to która ze zmiennych x,y,z ma uczestniczyć w tej podmianie var1 na 11 wg ciebie?

#include <iostream>
using namespace std;

class Test
  {
   public:
   int var1;
   Test(int var1=10):var1(var1) {}
   void metoda1()const { cout<<var1<<endl; }

   class b
     {
      public:
      void metoda2(Test &t)const
        {
         t.var1=11;
         t.metoda1();
        }
     };
  };
  
int main()
  {
   Test x(10);
   x.metoda1();
   Test::b().metoda2(x);
   x.metoda1();
   return 0;
  }

Jeżeli to nie jest to o co ci chodziło to poczytaj o dziedziczeniu.

1

błąd polega na tym że w klasie B nie ma czegoś takiego jak Test

Test->var1=11;
Test->metoda1(); 

w klasie b trzeba napisać Test *test i te testy powyżej zmienić na test (z Test). Usuń średniki przy klamrach zamykających metod. One powinny być tylko przy klasach

0

Dzięki już działa.

Pozdawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1