470,586 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Constructing Nested class

1
Hi

I have probleam while while initilazing nested class ...

I needs to create a object 'b' of B(nested class) in A(surrounding class).

while constructing b I have to pass pointer of funtion in Class A.

But when I am doing this compilr throws error.

Please helpe me to solve this problem...

here is the code...

#include<iostream.h>
#include<stdlib.h>
class A {

int j;

public:

class B {

int i ;

public:

B(){
cout<< "B is created \n";
}

B(void (*pf)()) {
cout<< "B is created....funp \n";
}

B(int i) {

cout<< "B is created...i \n";
}

void printB() {

cout<<"hai..B";
}


};


static int i;

B b;
B c;

A():b(show),c(j){ // <=======error is here
}

A(void (*pf)()) {

}

void show () {
cout<<"hello";
}
};


void show () {
cout<<"hello";
}

int main() {

int i;

A o(show);
A c(show);
A a;
}


Nabil.
Mar 28 '07 #1
1 1228
gpraghuram
1,275 Expert 1GB
HI,
You have to make following changes...
1)First the variable declaration B b should be changed to void (*b)();
2)Variable type of B c; should be changed.
3)Write a defaukt constructor for A.

I have made same changes and here is my code
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. class A {
  5. int j;
  6. public:
  7.  
  8.     class B 
  9.     {
  10.         int i ;
  11.         public:
  12.         B(){
  13.         cout<< "B is created \n";
  14.         }
  15.         B(void (*pf)()) {
  16.         cout<< "B is created....funp \n";
  17.         }
  18.         B(int i) {
  19.         cout<< "B is created...i \n";
  20.         }
  21.         void printB() {
  22.         cout<<"hai..B";
  23.         }
  24.     };
  25.  
  26.  
  27. static int i;
  28. void (*b)();
  29. //B b;
  30. //B c;
  31.  
  32.     A(void (*show)()):b(show)
  33.     { // <=======error is here
  34.     }
  35.     A()
  36.     {
  37.     }
  38.  
  39.     void show () {
  40.     cout<<"hello";
  41.     }
  42. };
  43. void show () {
  44.     cout<<"hello";
  45.     }
  46.  
  47. int main() {
  48.  
  49. int i;
  50.  
  51. A o(show);
  52. A c(show);
  53. A a;
  54.  
  55.  
Thanks
Raghuram
Mar 28 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Erik Bongers | last post: by
2 posts views Thread by newbiecpp | last post: by
6 posts views Thread by B0nj | last post: by
8 posts views Thread by Robert W. | last post: by
1 post views Thread by Anjo Gasa | last post: by
3 posts views Thread by jdurancomas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.