calling inherited constructors

Discussion in 'C++' started by Joe, G.I., Sep 22, 2008.

  1. Joe, G.I.

    Joe, G.I. Guest

    I didn't write ClassA, but I'm trying to inherit from ClassA through
    ClassB. I want to pass a string to the ClassA constructor but not sure
    I'm calling ClassA's constructor correctly. I get the following g++
    error ...

    In constructor 'ClassB::ClassB(String&)':
    ../../../include/ClassB.h:19: error: expected `{' at end of input


    ClassB *myClass = new ClassB("myClassName");

    // ClassB.h
    class ClassB : public ClassA {
    ClassB(String &name) : ClassA(name);
    }

    // ClassB.cpp
    ClassB::ClassB(String &name)
    {
    }

    // ClassA constructor which I'm trying to pass the string to ...
    ClassA::ClassA (const String & name)



    What am I doing wrong here?
     
    Joe, G.I., Sep 22, 2008
    #1
    1. Advertising

  2. Joe, G.I.

    Ian Collins Guest

    Joe, G.I. wrote:
    > I didn't write ClassA, but I'm trying to inherit from ClassA through
    > ClassB. I want to pass a string to the ClassA constructor but not sure
    > I'm calling ClassA's constructor correctly. I get the following g++
    > error ...
    >
    > In constructor 'ClassB::ClassB(String&)':
    > ../../../include/ClassB.h:19: error: expected `{' at end of input
    >
    >
    > ClassB *myClass = new ClassB("myClassName");
    >
    > // ClassB.h
    > class ClassB : public ClassA {
    > ClassB(String &name) : ClassA(name);
    > }
    >

    class ClassB : public ClassA
    {
    ClassB(String &name);
    };

    > // ClassB.cpp
    > ClassB::ClassB(String &name)
    > {
    > }
    >

    ClassB::ClassB(String &name)
    : ClassA( name )
    {
    }

    --
    Ian Collins.
     
    Ian Collins, Sep 23, 2008
    #2
    1. Advertising

  3. Joe, G.I.

    Andre Kostur Guest

    "Joe, G.I." <invalid.email@address> wrote in
    news::

    > I didn't write ClassA, but I'm trying to inherit from ClassA through
    > ClassB. I want to pass a string to the ClassA constructor but not sure
    > I'm calling ClassA's constructor correctly. I get the following g++
    > error ...
    >
    > In constructor 'ClassB::ClassB(String&)':
    > ../../../include/ClassB.h:19: error: expected `{' at end of input
    >
    >
    > ClassB *myClass = new ClassB("myClassName");
    >
    > // ClassB.h
    > class ClassB : public ClassA {
    > ClassB(String &name) : ClassA(name);
    > }
    >
    > // ClassB.cpp
    > ClassB::ClassB(String &name)
    > {
    > }
    >
    > // ClassA constructor which I'm trying to pass the string to ...
    > ClassA::ClassA (const String & name)
    >
    >
    >
    > What am I doing wrong here?


    Assuming you fix the minor issues (like the missing semicolon at the end of
    the class declaration), the piece you've got wrong is that the initializer
    list goes with the constructor definition, not the declaration:

    class ClassB : public ClassA {
    ClassB(String & name);
    };

    ClassB::ClassB(String & name) : ClassA(name) {}


    Something else to consider... do you really want to pass the String by non-
    const reference? Generally speaking, if you're passing stuff by reference,
    you should probably pass it by const-reference (unless you have a specific
    reason to pass it non-const...)
     
    Andre Kostur, Sep 23, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chris sennitt

    constructors not inherited ?

    chris sennitt, Mar 6, 2004, in forum: Java
    Replies:
    8
    Views:
    11,787
    Tor Iver Wilhelmsen
    Mar 6, 2004
  2. Brian Genisio

    inherited constructors

    Brian Genisio, Jan 5, 2004, in forum: C++
    Replies:
    10
    Views:
    8,294
    jeffc
    Jan 9, 2004
  3. Justin
    Replies:
    6
    Views:
    531
    Kaz Kylheku
    Nov 4, 2005
  4. Replies:
    20
    Views:
    773
    Gabriel
    Dec 1, 2005
  5. 7stud --
    Replies:
    11
    Views:
    414
    7stud --
    Nov 9, 2007
Loading...

Share This Page