Can't access WHAT private member?

B

Bruce

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;
}
 
D

David White

Bruce said:
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
 
B

Bruce

In comp.lang.c++

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;
}
 
D

David White

David White said:
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
 
B

Bruce

In comp.lang.c++
David White said:
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.
 
B

Bruce

In comp.lang.c++
David White said:
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?
 
A

Andrey Tarasevich

Bruce said:
...
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;
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;
 
O

osmium

Bruce said:
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.
 
K

Karl Heinz Buchegger

Bruce said:
In comp.lang.c++


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.
 

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

Similar Threads

Struct Member Variables Problem 0
Struct Member Variable Problems 1
Crossword 2
Space Invaders 0
My Status, Ciphertext 2
Can't solve problems! please Help 0
Crossword 14
Declaring a static const member of a class. 4

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top