473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can't access WHAT private member?

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
8 1813
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
"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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.