Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling a member method from the constructor

Reply
Thread Tools

Calling a member method from the constructor

 
 
John Harrison
Guest
Posts: n/a
 
      02-15-2007
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
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      02-15-2007
On 15 Feb, 20:30, "The Cool Giraffe" <(E-Mail Removed)> 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-lit...t.html#faq-5.8

Gavin Deane

 
Reply With Quote
 
 
 
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-15-2007
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)


 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-16-2007
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)


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-16-2007
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-16-2007
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-16-2007
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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-17-2007
"The Cool Giraffe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling another constructor from a constructor Asfand Yar Qazi C++ 6 05-17-2004 03:16 PM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments