LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2010, 06:39 AM   #1
jayasekar
Member
 
Registered: May 2008
Posts: 39

Rep: Reputation: 16
Lightbulb C++, How to initialize nested class constructor??


Hi,
class one
{
int xvalue;
public:
one(int x)
{
xvalue=x;
}
class nestedone
{
int ivalue;
public:
nestedone()
{
ivalue=0;
}
void function();
}obj1;
};

void one::nestedone::function()
{
cout<<"Iam in nestedone function\n";
}
int main(void)
{
one obj(1);
obj.obj1.function();
return 0;
}
My question is
1. If i replace B(int j){jvalue=i;} with B(){jvalue=0;}, it is working fine,
otherwise it is giving compilation error:

error: no matching function for call to 'one::nestedone::nestedone()’
note: candidates are: one::nestedone::nestedone(int)
note: one::nestedone::nestedone(const one::nestedone&)

what does the above error msg mean ??

2. can we have constructor for the nested class
3. If so, how to initialize the inner class constructor
Thanks in advance
 
Old 03-31-2010, 07:08 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Its difficult to read your code, kindly use code tags with proper indentation for your code !
 
Old 03-31-2010, 07:46 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
It's not clear what you are trying to do. But there needs to be something in the outer class constructor to provide the value for the inner class constructor. Here is my guess at what you might be looking for:
Code:
class one
{
   int xvalue;
   public:

   one(int outer_value, int inner_value)
   :
      xvalue( outer_value ),
      obj1( inner_value )
   {}

   class nestedone
   {
      int ivalue;
      public:

      nestedone( int value )
      :
         ivalue( value )
      {}

      void function();
   }obj1;
};

void one::nestedone::function()
{
   cout<<"Iam in nestedone function\n";
}
int main(void)
{
   one obj(1, 2);
   obj.obj1.function();
   return 0;
}
In case you haven't seen the member initialization syntax of C++ before, I'll give a short explanation. This code:
Code:
      nestedone( int value )
      :
         ivalue( value )
      {}
behaves the save as this code:
Code:
      nestedone( int value )
      {
         ivalue = value;
      }
Either works equally well for that constructor, so changing that constructor was just a matter of coding style.

But in the outer constructor, using the member initialization syntax was more important. Once nestedone's constructor is defined such that the parameter is required, the outer constructor must initialize obj1 before the opening "{" of its body.

Last edited by johnsfine; 03-31-2010 at 08:09 AM.
 
Old 04-01-2010, 01:25 AM   #4
jayasekar
Member
 
Registered: May 2008
Posts: 39

Original Poster
Rep: Reputation: 16
hi johnsfine, thanks for you help. What you have given is what i expected. Thanks for your help.
 
Old 04-02-2010, 12:18 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved to Programming
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
fopen fclose in class constructor bedoni Programming 9 03-26-2006 10:25 AM
Undefined references to vtable in class constructor? RavenOfOdin Programming 2 03-01-2006 05:46 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration