Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   help in constructor needed (http://www.velocityreviews.com/forums/t743885-help-in-constructor-needed.html)

DanielJohnson 02-20-2011 08:30 AM

help in constructor needed
 
Hi,

I am a newbie C++ programmer, so please pardon my ignorance. I am
creating a class like shown below.

In the Class B on lines marked by asterisk(*), the compiler complains
saying "Error: expected a type specifier".

This is in visual studio compiler on windows. I tried running the same
on g++ and and g++ complained "error: expected identifier before
"false""

Can you please help me ? I am not able to figure out anything form
these error messages.


#include<iostream>
using namespace std;

namespace A
{
class A
{
public:
A()
{
std::cout<<"\n default ctor";
}

A(bool _d)
{
d = _d;
std::cout<<"\n parametrized ctor";
}

private:
bool d;
};

class B
{
public:
// this uses a1 and a2 defined below
void some_func() { }
private:
A a1(false); // This is where compiler is complaining ************
A a2(true); // This is where compiler is complaining ************
};
}

Ian Collins 02-20-2011 08:37 AM

Re: help in constructor needed
 
On 02/20/11 09:30 PM, DanielJohnson wrote:
> Hi,
>
> I am a newbie C++ programmer, so please pardon my ignorance. I am
> creating a class like shown below.
>
> In the Class B on lines marked by asterisk(*), the compiler complains
> saying "Error: expected a type specifier".
>
> This is in visual studio compiler on windows. I tried running the same
> on g++ and and g++ complained "error: expected identifier before
> "false""
>
> Can you please help me ? I am not able to figure out anything form
> these error messages.
>
>
> #include<iostream>
> using namespace std;
>
> namespace A
> {
> class A
> {
> public:
> A()
> {
> std::cout<<"\n default ctor";
> }
>
> A(bool _d)
> {
> d = _d;
> std::cout<<"\n parametrized ctor";
> }
>
> private:
> bool d;
> };
>
> class B
> {
> public:
> // this uses a1 and a2 defined below
> void some_func() { }
> private:
> A a1(false); // This is where compiler is complaining ************
> A a2(true); // This is where compiler is complaining ************


You can't initialise member variables like that. Initialise them in B's
constructor.

B() : a1(false), a2(false) {}

--
Ian Collins

thomas 02-20-2011 09:17 AM

Re: help in constructor needed
 
On Feb 20, 4:30*pm, DanielJohnson <diffuse...@gmail.com> wrote:
> Hi,
>
> I am a newbie C++ programmer, so please pardon my ignorance. I am
> creating a class like shown below.
>
> In the Class B on lines marked by asterisk(*), the compiler complains
> saying "Error: expected a type specifier".
>
> This is in visual studio compiler on windows. I tried running the same
> on g++ and and g++ complained "error: expected identifier before
> "false""
>
> Can you please help me ? I am not able to figure out anything form
> these error messages.
>
> #include<iostream>
> using namespace std;
>
> namespace A
> {
> * * * * class A
> * * * * {
> * * * * public:
> * * * * * * * * A()
> * * * * * * * * {
> * * * * * * * * * * * * std::cout<<"\n default ctor";
> * * * * * * * * }
>
> * * * * * * * * A(bool _d)
> * * * * * * * * {
> * * * * * * * * * * * * d = _d;
> * * * * * * * * * * * * std::cout<<"\n parametrized ctor";
> * * * * * * * * }
>
> * * * * private:
> * * * * * * * * bool d;
> * * * * };
>
> * * * * class B
> * * * * {
> * * * * public:
> * * * * * * * * // this uses a1 and a2 defined below
> * * * * * * * * void some_func() { }
> * * * * private:
> * * * * * * * * A a1(false); *// This is where compiler is complaining ************
> * * * * * * * * A a2(true); * // This is where compiler is complaining ************
> * * * * };
>
> }
>
>


class A
{
int x = 2;
};

The compiler will complain because only const static member can be
initialized in-class.

The compiler process the code like below:
1) check each member in class A and allocate memory for each member.
if the member is const numeric type, the value is stored in symbol
table and no memory allocated needed.
if the member is of other type, memory allocation is required for it.
2) initialize class A with constructor.

Jorgen Grahn 02-20-2011 09:20 AM

Re: help in constructor needed
 
On Sun, 2011-02-20, DanielJohnson wrote:
> Hi,
>
> I am a newbie C++ programmer, so please pardon my ignorance. I am
> creating a class like shown below.

....
> class A
> {
> public:

....
> A(bool _d) { ...


Not related to your question, but anyway:

That should probably be 'explicit A(bool _d)'. Always use 'explicit'
on such constructors unless you have special reasons not to.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Juha Nieminen 02-20-2011 03:38 PM

Re: help in constructor needed
 
Ian Collins <ian-news@hotmail.com> wrote:
>> A a1(false); // This is where compiler is complaining ************
>> A a2(true); // This is where compiler is complaining ************

>
> You can't initialise member variables like that.


Btw, isn't the new standard going to change that?

Juha Nieminen 02-21-2011 02:29 PM

Re: help in constructor needed
 
Pete Becker <pete@versatilecoding.com> wrote:
> On 2011-02-20 10:38:28 -0500, Juha Nieminen said:
>
>> Ian Collins <ian-news@hotmail.com> wrote:
>>>> A a1(false); // This is where compiler is complaining ************
>>>> A a2(true); // This is where compiler is complaining ************
>>>
>>> You can't initialise member variables like that.

>>
>> Btw, isn't the new standard going to change that?

>
> No.


So this is something else completely?

http://en.wikipedia.org/wiki/C%2B%2B...on_improvement


All times are GMT. The time now is 01:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.