By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,710 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,710 IT Pros & Developers. It's quick & easy.

Calling a member method from the constructor

P: n/a
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)
Feb 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Feb 15 '07 #2

P: n/a
On 15 Feb, 20:30, "The Cool Giraffe" <giraf...@viltersten.comwrote:
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

Feb 15 '07 #3

P: n/a
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)
Feb 16 '07 #4

P: n/a
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

Feb 16 '07 #5

P: n/a
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
Feb 16 '07 #6

P: n/a
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
Feb 16 '07 #7

P: n/a
"The Cool Giraffe" <gi******@viltersten.comwrote in message
news:53*************@mid.individual.net...
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)

Feb 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.