Classes, Pointers, Constructors, Headaches 
July 22nd, 2005, 06:11 AM
| | | |
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? | 
July 22nd, 2005, 06:11 AM
| | | | 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. | 
July 22nd, 2005, 06:11 AM
| | | | 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) { | 
July 22nd, 2005, 06:12 AM
| | | | 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. | 
July 22nd, 2005, 06:12 AM
| | | | 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. | 
July 22nd, 2005, 06:12 AM
| | | | 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. | 
July 22nd, 2005, 06:12 AM
| | | | 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 | 
July 22nd, 2005, 06:13 AM
| | | | 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! |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|