help in constructor needed

D

DanielJohnson

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 ************
};
}
 
I

Ian Collins

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) {}
 
T

thomas

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.
 
J

Jorgen Grahn

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
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top