473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1372
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

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

Similar topics

3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
1
by: Anjo Gasa | last post by:
If I have the following class member: std::vector<floatsingleVector; I can initialize it to have 50 elements with values of 0.5: singleVector = std::vector<float>( 50, 0.5 ); My question...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.