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

Class in class problem

P: n/a
Yes, it's Xmas and I'm working...

Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);

public:
...
};

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter inside
CPlatform, but no error when I do the same just underneath it (global variable).

What's going on?

thanks
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Dec 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

You can't do it that way, you have to do it when constructing the class:

CPlatform::CPlatform():
actuator0(0)
{

}

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Dirk Bruere at Neopax" <di*********@gmail.com> wrote in message
news:41*************@individual.net...
Yes, it's Xmas and I'm working...

Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);

public:
... };

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter
inside CPlatform, but no error when I do the same just underneath it
(global variable).

What's going on?

thanks
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org

Dec 23 '05 #2

P: n/a
Dirk Bruere at Neopax wrote:
Yes, it's Xmas and I'm working...
It's not Xmas yet.
Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);
This is an attempt to initialise a non-static member where there can be no
initialiser. Read about initialising members properly.
public:
...
};

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter
inside CPlatform, but no error when I do the same just underneath it
(global variable).

What's going on?


Nothing. Learn C++. Read about constructors and initialiser lists.

V
Dec 23 '05 #3

P: n/a
Moonlit wrote:
Hi,

You can't do it that way, you have to do it when constructing the class:

CPlatform::CPlatform():
actuator0(0)
{

}


In that case it might be more transparent to use the default constructor:

class CPlatform
{
private:
CActuator actuator0;

public:
CPlatform(){actuator0.SetNumber(0);}
};

Any problems doing it this way?

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Dec 23 '05 #4

P: n/a
Victor Bazarov wrote:
Dirk Bruere at Neopax wrote:
Yes, it's Xmas and I'm working...

It's not Xmas yet.
Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);

This is an attempt to initialise a non-static member where there can be
no initialiser. Read about initialising members properly.
public:
... };

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter
inside CPlatform, but no error when I do the same just underneath it
(global variable).

What's going on?

Nothing. Learn C++. Read about constructors and initialiser lists.


Well, I have a lot of things to do.
Since I'm the tech guy in a startup company specialising in new VR tech I have
to be a Jack of All Trades. I'm doing the vision system (optics), motion control
(h/w electronic design and realtime s/w, plus mech), and audio DSP programming.
When we can afford a C++ coding monkey we'll get one.

Meanwhile, I'll ask around if I get a non-obvious (to me) problem.
Thanks.

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Dec 23 '05 #5

P: n/a
Dirk Bruere at Neopax wrote:
[..]
Well, I have a lot of things to do.
[..] When we can afford a C++ coding monkey we'll get
one.

Meanwhile, I'll ask around if I get a non-obvious (to me) problem.


Well, since you put it so nicely, I can only tell you to go have
a good holiday season. Best wishes from a C++ coding monkey.
Dec 23 '05 #6

P: n/a
Victor Bazarov wrote:
Dirk Bruere at Neopax wrote:
[..]
Well, I have a lot of things to do.
[..] When we can afford a C++ coding monkey we'll get
one.

Meanwhile, I'll ask around if I get a non-obvious (to me) problem.

Well, since you put it so nicely, I can only tell you to go have
a good holiday season. Best wishes from a C++ coding monkey.


And a merry Yuletime to you too:-)

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Dec 24 '05 #7

P: n/a
On Fri, 23 Dec 2005 22:10:35 +0000, Dirk Bruere at Neopax wrote:
Yes, it's Xmas and I'm working...

Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);

public:
...
};

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter inside
CPlatform, but no error when I do the same just underneath it (global variable).

What's going on?

thanks
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org

You can't initialize non static non-integer members inside the class
definition.

Your actuator is not static, so you should initialize it in the CPlatform
constructor:

CPlatform::CPlatform() : actuator0(0)
{
// more stuff in the constructor.
}
Dec 24 '05 #8

P: n/a
Hi,

Should work too, still I think mine is better ;-). Well there is less to
type and it might be more obvious for othere C++ developers. If you also
maintain the actutator class you might consider making the 0 the default
value for the constructor

class CAcutator
{
private:
unsigned long Number;
public:
CActuator( unsigned long Number = 0 ):
Number( Number )
{
}

Or something similar. Then you can forget about everything in the CPlatform
class

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Dirk Bruere at Neopax" <di*********@gmail.com> wrote in message
news:41*************@individual.net...
Moonlit wrote:
Hi,

You can't do it that way, you have to do it when constructing the class:

CPlatform::CPlatform():
actuator0(0)
{

}


In that case it might be more transparent to use the default constructor:

class CPlatform
{
private:
CActuator actuator0;

public:
CPlatform(){actuator0.SetNumber(0);}
};

Any problems doing it this way?

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org

Dec 24 '05 #9

P: n/a
Amadeus W. M. wrote:
On Fri, 23 Dec 2005 22:10:35 +0000, Dirk Bruere at Neopax wrote:

Yes, it's Xmas and I'm working...

Got a problem (of course).
*****

class CPlatform
{
private:
CActuator actuator0(0);

public:
...
};

CActuator actuator0(0);

I get errors when I try to construct a class variable with a parameter inside
CPlatform, but no error when I do the same just underneath it (global variable).

What's going on?

thanks
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org


You can't initialize non static non-integer members inside the class
definition.

Your actuator is not static, so you should initialize it in the CPlatform
constructor:

CPlatform::CPlatform() : actuator0(0)
{
// more stuff in the constructor.
}


Thanks.
Amadeus your real name?
Pretty good if so...
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Dec 24 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.