Calling a member method from the constructor

J

John Harrison

The said:
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
 
G

Gavin Deane

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
 
T

The Cool Giraffe

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?
 
T

The Cool Giraffe

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

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 () {}
 
K

Kai-Uwe Bux

The said:
John Harrison wrote/skrev/kaita/popisal/schreibt :

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


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


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
 
J

John Harrison

Kai-Uwe Bux said:
The Cool Giraffe wrote:




Gel ( void );




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
 
K

Kai-Uwe Bux

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
 
J

Jim Langston

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

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


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


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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top