calling inherited constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe, G.I.

    #1

    calling inherited constructors

    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("myClass Name");

    // 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?
  • Ian Collins

    #2
    Re: calling inherited constructors

    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("myClass Name");
    >
    // 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.

    Comment

    • Ruben

      #3
      Re: calling inherited constructors

      On Mon, 22 Sep 2008 22:58:11 +0000, Joe, G.I. wrote:
      >
      ClassB *myClass = new ClassB("myClass Name");
      >
      // ClassB.h
      class ClassB : public ClassA {
      ClassB(String &name) : ClassA(name);
      }
      }
      // ClassB.cpp
      ClassB::ClassB( String &name)
      {
      }
      }

      There are so many things wrong here its hard to figure what your trying to
      do. First, the >class< declaration in the .h file needs a semi-colon at
      the end.

      And I think its likely that you don't have matching curley braces.

      Ruben
      --
      http://www.mrbrklyn.com - Interesting Stuff
      http://www.nylxs.com - Leadership Development in Free Software

      So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

      http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

      "Yeah - I write Free Software...so SUE ME"

      "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

      "I'm an engineer. I choose the best tool for the job, politics be damned.<
      You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

      © Copyright for the Digital Millennium

      Comment

      • Ruben

        #4
        Re: calling inherited constructors

        On Mon, 22 Sep 2008 22:58:11 +0000, Joe, G.I. wrote:
        ClassB(String &name) : ClassA(name);
        I think there is no semi-colon on the initialization list because it
        should be followed up by ClassB()'s {}'s



        It should look something like this in the definition of the class ClassB

        Bugtrap::Bugtra p(const string errormsg, const string file, const string line) : runtime_error(e rrormsg.c_str() )
        {
        cerr << what();
        cerr << "\nError file: " << file << " and line: " << line;
        cerr << "\n**exitin g now**\n";

        }




        --
        http://www.mrbrklyn.com - Interesting Stuff
        http://www.nylxs.com - Leadership Development in Free Software

        So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

        http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

        "Yeah - I write Free Software...so SUE ME"

        "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

        "I'm an engineer. I choose the best tool for the job, politics be damned.<
        You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

        © Copyright for the Digital Millennium

        Comment

        • Andre Kostur

          #5
          Re: calling inherited constructors

          "Joe, G.I." <invalid.email@ addresswrote in
          news:gb9pvt02l9 e@news4.newsguy .com:
          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("myClass Name");
          >
          // 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...)

          Comment

          Working...