Classes, Pointers, Constructors, Headaches

M

Mei

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?
 
J

jeffc

Mei said:
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?

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.
 
D

David Harmon

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.

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

jeffc

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

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

David Harmon

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

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

jeffc

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

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

Andrew Heath

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

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
 
J

jeffc

Andrew Heath said:
You probably meant:
B::B(int ii, A& aa) : a(aa) {


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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top