Thread: passing constructor data to other constructors

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    passing constructor data to other constructors

    I am writing a project which uses inheritence.

    My base class has 2 constructors. One constructor using no values and one overloaded constructor which takes 5 values
    Code:
       Sensor();
       Sensor(int, int, int, float, float);
    My derrived classes also have the same setup but take additional values e.g.
    Code:
       typeC();
       typeC(float, float );
    When instantiating an object for a derrived class passing values, I need to pass the correct values for that particular class, plus the values for the base class, which will in turn be passed to the base class.

    How do you do this?

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Provide a constructor for the inherited class that takes the parameters you want. So in your case, the constructor would have to take seven parameters I guess.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's a simple example: Initializing Base Classes

    gg

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Can someone please help me with this I've been trying to sort this one bit for over 12 hours now

    I'm trying to populate my program with data using overloaded constructors to input the data when the program starts. However I can't seem to get it to work.

    In the event that I didn't post enough code on here to help me with, I put my project in the net >HERE<

    The code to instantiate the objects is at the bottom of ControlCentre.cpp which is sensors[Sensor::GetSensorCount() + 1] = new typeB(1, 2, 3, 4, 5, 6);

    This should then call the paramaterised constructor and instantiate a new object with my values. I just can't seem to get it to work.
    I know its not as simple as a regular one due to the inheritence as data also needs to be passed back to the base class, i'm just a bit lost with it all.

    If someone could just have a look at 'typeB' and get that running I can hopefully understand how to do it then and do the other classes myself.


    I would be very appricietve if anyone could quicky sort this for me.

    thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    For one,
    Code:
    typeB::typeB(int air, int winds, int windd, float lon, float la, int sun) : Sensor(int air, int winds, int windd, float lon, float la) {
    should be
    Code:
    typeB::typeB(int air, int winds, int windd, float lon, float la, int sun) : Sensor(air, winds, windd, lon, la) {
    in the typeB constructor definition.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    To elaborate on jlou's post, the reason for the correction is that it's a call to the Sensor constructor, not a definition/declaration, so you don't specify a type.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    When I try that I get the following error messages:
    In file included from typeB.cpp:2:
    Sensor.h:57: function body for constructor missing

    Sensor.h: In constructor `typeB::typeB(int, int, int, float, float, int)':
    Sensor.h:57: parse error before `;' token

    typeB.cpp: At global scope:
    typeB.cpp:15: redefinition of `typeB::typeB(int, int, int, float, float, int)'

    Sensor.h:57: `typeB::typeB(int, int, int, float, float, int)' previously
    defined here

    typeB.cpp:15: no `typeB::typeB(int, int, int, float, float, int)' member
    function declared in class `typeB'

    make.exe: *** [typeB.o] Error 1

    Execution terminated
    so I tried removing the data types from the typeB part too
    Code:
    typeB::typeB(air, winds, windd, lon, la, sun) : Sensor(air, winds, windd, lon, la) {
    but this gives me the following error:
    In file included from typeB.cpp:2:
    Sensor.h:57: function body for constructor missing

    Sensor.h: In constructor `typeB::typeB(int, int, int, float, float, int)':
    Sensor.h:57: parse error before `;' token

    typeB.cpp: At global scope:
    typeB.cpp:15: parse error before `,' token

    typeB.cpp:17: ISO C++ forbids declaration of `sensor_type' with no type

    typeB.cpp:18: syntax error before `<<' token

    make.exe: *** [typeB.o] Error 1

    Execution terminated
    Does anyone know whats going wrong here?

    I've tried my class declaration as both
    Code:
    typeB(int, int, int, float, float, int ) : Sensor();
    and
    Code:
    typeB(int, int, int, float, float, int ) : Sensor(int, int, int, float, float, int);
    Thanks
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think I know what's happening. In the class declaration, do this:
    Code:
    public:
    typeB(int,int,int,float,float,int);
    Then in the cpp file, do this:
    Code:
    typeB::typeB(int air,int winds,int windd,float lon,float la,int sun):Sensor(air,winds,windd,lon,la)
    {
    }
    That should fix it.

    EDIT: The point is, I don't think you can call a base class constructor from an initializer in the class declaration.
    Last edited by bennyandthejets; 05-21-2004 at 01:45 AM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Thanks Benny
    You have just made my day
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Thank you. You forced me to work that out, and now I know how to do it.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM