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

Can't access WHAT private member?

P: n/a
OK, this won't compile saying it can't access private members declared in
class F. I don't get it and even if I make the entire class public, it
still says that. I realize it has something to do with the constructors
but not what.

Second, I'd like to take the line:

// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

And add that as private data to Class B but it fails saying "syntax error
constant"

I'm really trying to avoid using an array of classes but they're making it
difficult on me.


# include <vector>
# include <string>

using namespace std;

class F
{
F() { X = 0; Y = 0; P = 0;};
F( unsigned long x, unsigned long y) {X = x; Y = y; P = x * y;};
~F();

private:
unsigned long X;
unsigned long Y;
unsigned long P;
};

class B
{
B() {strValue = "1";};
B( string &val){ strValue = val;};

private:
string strValue;
};
int main()
{
// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

// do something else
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Bruce writes:
OK, this won't compile saying it can't access private members declared in
class F. I don't get it and even if I make the entire class public, it
still says that. I realize it has something to do with the constructors
but not what.

Second, I'd like to take the line:

// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

And add that as private data to Class B but it fails saying "syntax error
constant"

I'm really trying to avoid using an array of classes but they're making it
difficult on me.


# include <vector>
# include <string>

using namespace std;

class F
{
F() { X = 0; Y = 0; P = 0;};
F( unsigned long x, unsigned long y) {X = x; Y = y; P = x * y;};
~F();
The three above are private too. Private is the default for a class.

private:
unsigned long X;
unsigned long Y;
unsigned long P;
};

class B
{
B() {strValue = "1";};
B( string &val){ strValue = val;};

private:
string strValue;
};
int main()
{
// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

// do something else
return 0;
}

Jul 22 '05 #2

P: n/a
"Bruce" <br***@nospam.com> wrote in message
news:3f***************@news.dallas.sbcglobal.net.. .
OK, this won't compile saying it can't access private members declared in
class F. I don't get it and even if I make the entire class public, it
still says that. I realize it has something to do with the constructors
but not what.

Second, I'd like to take the line:

// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

And add that as private data to Class B but it fails saying "syntax error
constant"

I'm really trying to avoid using an array of classes but they're making it
difficult on me.


# include <vector>
# include <string>

using namespace std;

class F
{
public:

This fixes your "private" problem.
F() { X = 0; Y = 0; P = 0;};
F( unsigned long x, unsigned long y) {X = x; Y = y; P = x * y;};
~F();

private:
unsigned long X;
unsigned long Y;
unsigned long P;
};

class B
{
B() {strValue = "1";};
B( string &val){ strValue = val;};
Make these:
B() : m_f(1000, F(1,1)) {strValue = "1";};
B( string &val) : m_f(1000, F(1,1)) { strValue = val;};

private:
string strValue;
You have to give your vector<F> member a name, e.g.,
vector<F> m_f;

Member variables, including vectors, can only be intialized at construction
time (see constructors above), not where you declare the member.
};


DW

Jul 22 '05 #3

P: n/a
In comp.lang.c++
"osmium" <r1********@comcast.net> wrote:

Well, as soon as I posted it, I figured out I forgot the public tag. OK,
so now, how do I make this work:
# include <vector>
# include <string>

using namespace std;

class F
{
public:
F() { X = 0; Y = 0; P = 0;};
F( unsigned long x, unsigned long y) {X = x; Y = y; P = x * y;};
~F();

private:
unsigned long X;
unsigned long Y;
unsigned long P;
};

class B
{
public:
B() {strValue = "1";};
B( string &val){ strValue = val;};

private:
string strValue;
// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

};
int main()
{
B b();

// do something else
return 0;
}

Jul 22 '05 #4

P: n/a
"David White" <no@email.provided> wrote in message
news:Sf******************@nasal.pacific.net.au...
Make these:
B() : m_f(1000, F(1,1)) {strValue = "1";};
B( string &val) : m_f(1000, F(1,1)) { strValue = val;};


I was too pre-occupied with the vector to do the rest of it. Better would
be:
B() : strValue("1"), m_f(1000, F(1,1)) {}
B( string &val) : strValue(val), m_f(1000, F(1,1)) { }

This is better because, previously, strValue was constructed first with the
default constructor and assigned to another value later. Now it's
constructed with the right value to begin with.

Note also that there is no semi-colon after the }on functions.

DW

Jul 22 '05 #5

P: n/a
In comp.lang.c++
"David White" <no@email.provided> wrote:
public:

This fixes your "private" problem.
Yep, thanks.
Make these:
B() : m_f(1000, F(1,1)) {strValue = "1";};
B( string &val) : m_f(1000, F(1,1)) { strValue = val;};
You have to give your vector<F> member a name, e.g.,
vector<F> m_f;

Member variables, including vectors, can only be intialized at construction
time (see constructors above), not where you declare the member.


Very good, thanks a million. Makes sense now too.
Jul 22 '05 #6

P: n/a
In comp.lang.c++
"David White" <no@email.provided> wrote:
"David White" <no@email.provided> wrote in message
news:Sf******************@nasal.pacific.net.au. ..
Make these:
B() : m_f(1000, F(1,1)) {strValue = "1";};
B( string &val) : m_f(1000, F(1,1)) { strValue = val;};


I was too pre-occupied with the vector to do the rest of it. Better would
be:
B() : strValue("1"), m_f(1000, F(1,1)) {}
B( string &val) : strValue(val), m_f(1000, F(1,1)) { }


Funny, I just made that exact change. Why is that method of initialization
preferred?
Jul 22 '05 #7

P: n/a
Bruce wrote:
...
Well, as soon as I posted it, I figured out I forgot the public tag. OK,
so now, how do I make this work:

class B
{
public:
B() {strValue = "1";};
B( string &val){ strValue = val;};

private:
string strValue;
// Create an vector of 1000 F objects init'd to 1,1
vector<F> F(1000,F(1,1));

};
- Use member initialization lists in constructors
- Stop using member names that coincide with type names
- Don't use superfluous semicolons
- Use 'const' specifier whenever it's appropriate

class B
{
public:
B() : strValue("1"), f(1000, F(1, 1)) {}
B(const string &val) : strValue(val) {}

private:
string strValue;
vector<F> f;
};
int main()
{
B b();
This is function declaration, not an object definition. To define a
default-initialized object of type 'B' remove the '()'

B b;
// do something else
return 0;
}


--
Best regards,
Andrey Tarasevich

Jul 22 '05 #8

P: n/a
Bruce wrote:

In comp.lang.c++
"David White" <no@email.provided> wrote:
"David White" <no@email.provided> wrote in message
news:Sf******************@nasal.pacific.net.au. ..
Make these:
B() : m_f(1000, F(1,1)) {strValue = "1";};
B( string &val) : m_f(1000, F(1,1)) { strValue = val;};


I was too pre-occupied with the vector to do the rest of it. Better would
be:
B() : strValue("1"), m_f(1000, F(1,1)) {}
B( string &val) : strValue(val), m_f(1000, F(1,1)) { }


Funny, I just made that exact change. Why is that method of initialization
preferred?


Because it is an initialization, not a 'initialize to default and assign
a value later'.

In

B() { strValue = "1"; }

What is happening?

First strValue gets initialized when a B object comes to life. Since
nothing is specified in the initializer list, strValue initializes
itself to an empty string. Then the ctor body starts to run and
assigns the string "1" to strValue. Note that there are 2 actions
happening: first initialization then assignment.

On the other hand in

B() : strValue( "1" ) {}

only initialization occours: strValue is initialized with the string "1".
And that's it, nothing else occours.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.