Classes, Pointers, Constructors, Headaches

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mei

    Classes, Pointers, Constructors, Headaches

    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?
  • jeffc

    #2
    Re: Classes, Pointers, Constructors, Headaches


    "Mei" <mei@smokingcat erpillar.com> wrote in message
    news:54207363.0 401131050.42e2b 6cc@posting.goo gle.com...[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 "initialize r 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.


    Comment

    • David Harmon

      #3
      Re: Classes, Pointers, Constructors, Headaches

      On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
      mei@smokingcate rpillar.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) {

      Comment

      • jeffc

        #4
        Re: Classes, Pointers, Constructors, Headaches


        "David Harmon" <source@netcom. com> wrote in message
        news:402c486e.1 20593332@news.w est.earthlink.n et...[color=blue]
        > On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
        > mei@smokingcate rpillar.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.


        Comment

        • David Harmon

          #5
          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]
          >
          >Unfortunatel y, 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.

          Comment

          • jeffc

            #6
            Re: Classes, Pointers, Constructors, Headaches


            "David Harmon" <source@netcom. com> wrote in message
            news:400c6692.7 330766@news.wes t.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]
            > >
            > >Unfortunatel y, 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.


            Comment

            • Andrew Heath

              #7
              Re: Classes, Pointers, Constructors, Headaches

              jeffc wrote:[color=blue]
              > "David Harmon" <source@netcom. com> wrote in message
              > news:402c486e.1 20593332@news.w est.earthlink.n et...
              >[color=green]
              >>On 13 Jan 2004 10:50:52 -0800 in comp.lang.c++,
              >>mei@smokingca terpillar.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
              >>>instantiat e 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
              >>initializatio n 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

              Comment

              • jeffc

                #8
                Re: Classes, Pointers, Constructors, Headaches


                "Andrew Heath" <fake@email.com > wrote in message
                news:40049328$0 $4052$afc38c87@ news.optusnet.c om.au...[color=blue][color=green][color=darkred]
                > >>You have forgotten to call the constructor for A in your constructor
                > >>initializatio n 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!


                Comment

                Working...