Calling a member method from the constructor

Discussion in 'C++' started by John Harrison, Feb 15, 2007.

  1. The Cool Giraffe wrote:
    > I noticed that when i call a function foo everything
    > works as expected. The problem is that when i try to
    > call foo from the constructor when compiling, the
    > computer bugs me about unresolved external.
    >
    > Can i somehow declare a method so it's available for
    > the constructor?
    >


    There's no reason a method cannot be called from a constructor. You
    doesn't have to declare it any special way.

    There only reason I can think of for what you are describing is that you
    are trying to call a pure virtual method from a constructor, and
    expecting the derived class virtual method to be called, is that the
    case? Better post some code.

    john
     
    John Harrison, Feb 15, 2007
    #1
    1. Advertising

  2. John Harrison

    Gavin Deane Guest

    On 15 Feb, 20:30, "The Cool Giraffe" <> wrote:
    > I noticed that when i call a function foo everything
    > works as expected. The problem is that when i try to
    > call foo from the constructor when compiling, the
    > computer bugs me about unresolved external.
    >
    > Can i somehow declare a method so it's available for
    > the constructor?


    There's nothing special about calling a function from a constructor
    compared to calling the same function from anywhere else in your code
    (unless it's a virtual function in the class you're constructing, but
    you haven't mentioned anything about that).

    It's always better to post actual code rather than trying to describe
    what the code looks like in words.
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    Gavin Deane
     
    Gavin Deane, Feb 15, 2007
    #2
    1. Advertising

  3. I noticed that when i call a function foo everything
    works as expected. The problem is that when i try to
    call foo from the constructor when compiling, the
    computer bugs me about unresolved external.

    Can i somehow declare a method so it's available for
    the constructor?

    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Feb 15, 2007
    #3
  4. John Harrison wrote/skrev/kaita/popisal/schreibt :
    > The Cool Giraffe wrote:


    >> I noticed that when i call a function foo everything
    >> works as expected. The problem is that when i try to
    >> call foo from the constructor when compiling, the
    >> computer bugs me about unresolved external.
    >>
    >> Can i somehow declare a method so it's available for
    >> the constructor?

    >
    > There's no reason a method cannot be called from a constructor. You
    > doesn't have to declare it any special way.


    All, right, that's what expected. Good to have that said.

    > There only reason I can think of for what you are describing is that you
    > are trying to call a pure virtual method from a constructor...


    I don't think i do. The class isn't inheriting anything
    and i don't use the virtual keyword anywhere.

    > Better post some code.


    Here is the _REALLY_ stripped version still giving the
    same error. Do you get the same too?

    class Gel
    { public:
    Gel::Gel (void);
    void doWall (); };

    #include ".\Gel.h"
    Gel::Gel (void) {doWall ();}
    void doWall () {}


    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Feb 16, 2007
    #4
  5. John Harrison

    Kai-Uwe Bux Guest

    The Cool Giraffe wrote:

    > John Harrison wrote/skrev/kaita/popisal/schreibt :
    >> The Cool Giraffe wrote:

    >
    >>> I noticed that when i call a function foo everything
    >>> works as expected. The problem is that when i try to
    >>> call foo from the constructor when compiling, the
    >>> computer bugs me about unresolved external.
    >>>
    >>> Can i somehow declare a method so it's available for
    >>> the constructor?

    >>
    >> There's no reason a method cannot be called from a constructor. You
    >> doesn't have to declare it any special way.

    >
    > All, right, that's what expected. Good to have that said.
    >
    >> There only reason I can think of for what you are describing is that you
    >> are trying to call a pure virtual method from a constructor...

    >
    > I don't think i do. The class isn't inheriting anything
    > and i don't use the virtual keyword anywhere.
    >
    >> Better post some code.

    >
    > Here is the _REALLY_ stripped version still giving the
    > same error. Do you get the same too?
    >
    > class Gel
    > { public:
    > Gel::Gel (void);


    Gel ( void );

    > void doWall (); };
    >
    > #include ".\Gel.h"
    > Gel::Gel (void) {doWall ();}
    > void doWall () {}


    void Gel::doWall() {}


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 16, 2007
    #5
  6. Kai-Uwe Bux wrote:
    > The Cool Giraffe wrote:
    >
    >
    >>John Harrison wrote/skrev/kaita/popisal/schreibt :
    >>
    >>>The Cool Giraffe wrote:

    >>
    >>>>I noticed that when i call a function foo everything
    >>>>works as expected. The problem is that when i try to
    >>>>call foo from the constructor when compiling, the
    >>>>computer bugs me about unresolved external.
    >>>>
    >>>>Can i somehow declare a method so it's available for
    >>>>the constructor?
    >>>
    >>>There's no reason a method cannot be called from a constructor. You
    >>>doesn't have to declare it any special way.

    >>
    >>All, right, that's what expected. Good to have that said.
    >>
    >>
    >>>There only reason I can think of for what you are describing is that you
    >>>are trying to call a pure virtual method from a constructor...

    >>
    >>I don't think i do. The class isn't inheriting anything
    >>and i don't use the virtual keyword anywhere.
    >>
    >>
    >>>Better post some code.

    >>
    >>Here is the _REALLY_ stripped version still giving the
    >>same error. Do you get the same too?
    >>
    >>class Gel
    >>{ public:
    >> Gel::Gel (void);

    >
    >
    > Gel ( void );
    >
    >
    >> void doWall (); };
    >>
    >>#include ".\Gel.h"
    >>Gel::Gel (void) {doWall ();}
    >>void doWall () {}

    >
    >
    > void Gel::doWall() {}
    >
    >
    > Best
    >
    > Kai-Uwe Bux
    >


    Personally I don't even like to see (void). It's unnecessary in C++
    (though not C). So

    class Gel
    {
    public:
    Gel();
    void doWall();
    };

    Gel::Gel() { doWall(); }
    void Gel::doWall() {}

    john
     
    John Harrison, Feb 16, 2007
    #6
  7. John Harrison

    Kai-Uwe Bux Guest

    John Harrison wrote:
    [about function declarations/definitions]
    > Personally I don't even like to see (void). It's unnecessary in C++
    > (though not C).


    You maybe right about C (about which I know nothing). As far as C++ is
    concerned, I know that I could drop the ( void ) in declarations. However,
    I like the verbosity and I find that it slightly improves the grep-ability
    of the code. Mostly, it seems to be a matter of taste.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 16, 2007
    #7
  8. John Harrison

    Jim Langston Guest

    "The Cool Giraffe" <> wrote in message
    news:...
    > John Harrison wrote/skrev/kaita/popisal/schreibt :
    >> The Cool Giraffe wrote:

    >
    >>> I noticed that when i call a function foo everything
    >>> works as expected. The problem is that when i try to
    >>> call foo from the constructor when compiling, the
    >>> computer bugs me about unresolved external.
    >>>
    >>> Can i somehow declare a method so it's available for
    >>> the constructor?

    >>
    >> There's no reason a method cannot be called from a constructor. You
    >> doesn't have to declare it any special way.

    >
    > All, right, that's what expected. Good to have that said.
    >
    >> There only reason I can think of for what you are describing is that you
    >> are trying to call a pure virtual method from a constructor...

    >
    > I don't think i do. The class isn't inheriting anything
    > and i don't use the virtual keyword anywhere.
    >
    >> Better post some code.

    >
    > Here is the _REALLY_ stripped version still giving the
    > same error. Do you get the same too?
    >
    > class Gel
    > { public:
    > Gel::Gel (void);
    > void doWall (); };
    >
    > #include ".\Gel.h"
    > Gel::Gel (void) {doWall ();}


    At his point doWall hasn't be declared or defined.

    > void doWall () {}


    Here you declare nad define doWall, but after you needed it. It seems like
    all you need is a prototype higher up in the code.

    void doWall();
    beofre Gel::Gell()

    >
    >
    > --
    > Vänligen Kerstin Viltersten
    > (The Cool Giraffe)
    >
     
    Jim Langston, Feb 17, 2007
    #8
    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. Giulio
    Replies:
    9
    Views:
    1,047
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,337
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,828
    David Harmon
    Jan 23, 2004
  4. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,640
    jeffc
    May 17, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,248
Loading...

Share This Page