Connecting Tech Pros Worldwide Help | Site Map

Classes, Pointers, Constructors, Headaches

  #1  
Old July 22nd, 2005, 06:11 AM
Mei
Guest
 
Posts: n/a
I have some classes at the moment that are initialised with their own
function rather than a constructor and destructor. I wish this case to
be reversed if possible.

For arguments sake (you dont need or want the actual classes here the
shortened ones are long enough):-

class A {
int i;
public:
void init(int ii);
}

class B (
int i;
A a;
public:
void init(int ii,A& aa);
}

A::init(int ii) {
i=ii;
}

B::init(int ii,A& aa){
i=ii;
a=aa;
}

int main() {
A a;
B b;
a.init(1);
b.init(1,a);
}

B is successfully composed into A and both initiate fine. If i
replace the "void init" functions with constructors and deconstructors
like so :-

class A {
int i;
public:
A(int ii);
~A(){};
}

class B (
int i;
A a;
public:
B(int ii,A& aa);
~B(){};
}

A::A(int ii) {
i=ii;
}

B::B(int ii,A& aa){
i=ii;
a=aa;
}

Class B no longer works due to the compiler error "no matching
function for call to `" (I think there should be more to that), From
what i can gather though whats happening is that I am trying to
instantiate class A in the arguments for class B, rather than pass
class A.

What am i doing wrong?
  #2  
Old July 22nd, 2005, 06:11 AM
jeffc
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches



"Mei" <mei@smokingcaterpillar.com> wrote in message
news:54207363.0401131050.42e2b6cc@posting.google.c om...[color=blue]
> I have some classes at the moment that are initialised with their own
> function rather than a constructor and destructor. I wish this case to
> be reversed if possible.
>
> For arguments sake (you dont need or want the actual classes here the
> shortened ones are long enough):-
>
> class A {
> int i;
> public:
> void init(int ii);
> }
>
> class B (
> int i;
> A a;
> public:
> void init(int ii,A& aa);
> }
>
> A::init(int ii) {
> i=ii;
> }
>
> B::init(int ii,A& aa){
> i=ii;
> a=aa;
> }
>
> int main() {
> A a;
> B b;
> a.init(1);
> b.init(1,a);
> }
>
> B is successfully composed into A and both initiate fine. If i
> replace the "void init" functions with constructors and deconstructors
> like so :-
>
> class A {
> int i;
> public:
> A(int ii);
> ~A(){};
> }
>
> class B (
> int i;
> A a;
> public:
> B(int ii,A& aa);
> ~B(){};
> }
>
> A::A(int ii) {
> i=ii;
> }
>
> B::B(int ii,A& aa){
> i=ii;
> a=aa;
> }
>
> Class B no longer works due to the compiler error "no matching
> function for call to `" (I think there should be more to that), From
> what i can gather though whats happening is that I am trying to
> instantiate class A in the arguments for class B, rather than pass
> class A.
>
> What am i doing wrong?[/color]

Well, first you left a lot out. Before I speculate, why don't you show how
you changed your main to use this new code? Right off the top of my head I
see you have an error in your B constructor. You now have a special
constructor for A, but you're not invoking that constructor from B, so A
can't be constructed. You actually have a very confusing situation here. I
think you should read about "initializer lists". You need to invoke the
constructor for A in an initializer list from B's constructor, but you need
to send it the i value from the A you're sending into B. You don't have
access to that, because i is private in A. You're going to have to change
your design.


  #3  
Old July 22nd, 2005, 06:11 AM
David Harmon
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches


On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
mei@smokingcaterpillar.com (Mei) was alleged to have written:[color=blue]
>B::B(int ii,A& aa){
> i=ii;
> a=aa;
>}
>
>Class B no longer works due to the compiler error "no matching
>function for call to `" (I think there should be more to that), From
>what i can gather though whats happening is that I am trying to
>instantiate class A in the arguments for class B, rather than pass
>class A.[/color]

You have forgotten to call the constructor for A in your constructor
initialization list, ie. like:
B::B(int ii,A& aa): A(aa) {

  #4  
Old July 22nd, 2005, 06:12 AM
jeffc
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches



"David Harmon" <source@netcom.com> wrote in message
news:402c486e.120593332@news.west.earthlink.net...[color=blue]
> On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
> mei@smokingcaterpillar.com (Mei) was alleged to have written:[color=green]
> >B::B(int ii,A& aa){
> > i=ii;
> > a=aa;
> >}
> >
> >Class B no longer works due to the compiler error "no matching
> >function for call to `" (I think there should be more to that), From
> >what i can gather though whats happening is that I am trying to
> >instantiate class A in the arguments for class B, rather than pass
> >class A.[/color]
>
> You have forgotten to call the constructor for A in your constructor
> initialization list, ie. like:
> B::B(int ii,A& aa): A(aa) {[/color]

Unfortunately, he doesn't have a constructor for A that takes an A, so that
won't work.


  #5  
Old July 22nd, 2005, 06:12 AM
David Harmon
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches


On Tue, 13 Jan 2004 16:14:00 -0500 in comp.lang.c++, "jeffc"
<nobody@nowhere.com> was alleged to have written:[color=blue][color=green]
>> You have forgotten to call the constructor for A in your constructor
>> initialization list, ie. like:
>> B::B(int ii,A& aa): A(aa) {[/color]
>
>Unfortunately, he doesn't have a constructor for A that takes an A, so that
>won't work.[/color]

Yes, there are other matters I overlooked there too; but I think that is
the right direction to be headed.

  #6  
Old July 22nd, 2005, 06:12 AM
jeffc
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches



"David Harmon" <source@netcom.com> wrote in message
news:400c6692.7330766@news.west.earthlink.net...[color=blue]
> On Tue, 13 Jan 2004 16:14:00 -0500 in comp.lang.c++, "jeffc"
> <nobody@nowhere.com> was alleged to have written:[color=green][color=darkred]
> >> You have forgotten to call the constructor for A in your constructor
> >> initialization list, ie. like:
> >> B::B(int ii,A& aa): A(aa) {[/color]
> >
> >Unfortunately, he doesn't have a constructor for A that takes an A, so[/color][/color]
that[color=blue][color=green]
> >won't work.[/color]
>
> Yes, there are other matters I overlooked there too; but I think that is
> the right direction to be headed.[/color]

Looks like a "redo from scratch" problem to me.


  #7  
Old July 22nd, 2005, 06:12 AM
Andrew Heath
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches


jeffc wrote:[color=blue]
> "David Harmon" <source@netcom.com> wrote in message
> news:402c486e.120593332@news.west.earthlink.net...
>[color=green]
>>On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
>>mei@smokingcaterpillar.com (Mei) was alleged to have written:
>>[color=darkred]
>>>B::B(int ii,A& aa){
>>>i=ii;
>>>a=aa;
>>>}
>>>
>>>Class B no longer works due to the compiler error "no matching
>>>function for call to `" (I think there should be more to that), From
>>>what i can gather though whats happening is that I am trying to
>>>instantiate class A in the arguments for class B, rather than pass
>>>class A.[/color]
>>
>>You have forgotten to call the constructor for A in your constructor
>>initialization list, ie. like:
>> B::B(int ii,A& aa): A(aa) {[/color][/color]

You probably meant:
B::B(int ii, A& aa) : a(aa) {
[color=blue]
>
>
> Unfortunately, he doesn't have a constructor for A that takes an A, so that
> won't work.[/color]

The compiler generates a default copy constructor and operator= function
when one hasn't been supplied. Thus expressions like the following are
well formed:

A foo, bar;
...
foo = bar; // Invokes default operator=
...
A baz(foo); // Invokes default copy constructor

  #8  
Old July 22nd, 2005, 06:13 AM
jeffc
Guest
 
Posts: n/a

re: Classes, Pointers, Constructors, Headaches



"Andrew Heath" <fake@email.com> wrote in message
news:40049328$0$4052$afc38c87@news.optusnet.com.au ...[color=blue][color=green][color=darkred]
> >>You have forgotten to call the constructor for A in your constructor
> >>initialization list, ie. like:
> >> B::B(int ii,A& aa): A(aa) {[/color][/color]
>
> You probably meant:
> B::B(int ii, A& aa) : a(aa) {
>[color=green]
> > Unfortunately, he doesn't have a constructor for A that takes an A, so[/color][/color]
that[color=blue][color=green]
> > won't work.[/color]
>
> The compiler generates a default copy constructor and operator= function
> when one hasn't been supplied. Thus expressions like the following are
> well formed:
>
> A foo, bar;
> ...
> foo = bar; // Invokes default operator=
> ...
> A baz(foo); // Invokes default copy constructor[/color]

I stand corrected! I was absent-mindedly thinking of the default
constructor only, forgetting this was a copy constructor!


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Naming convention fit for C++ Carl Colijn answers 0 November 22nd, 2005 04:15 AM