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;
} 8 1822
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; }
"Bruce" <br***@nospam.c om> wrote in message
news:3f******** *******@news.da llas.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
In comp.lang.c++
"osmium" <r1********@com cast.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;
}
"David White" <no@email.provi ded> wrote in message
news:Sf******** **********@nasa l.pacific.net.a u... 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
In comp.lang.c++
"David White" <no@email.provi ded> 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.
In comp.lang.c++
"David White" <no@email.provi ded> wrote: "David White" <no@email.provi ded> wrote in message news:Sf******* ***********@nas al.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?
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
Bruce wrote: In comp.lang.c++ "David White" <no@email.provi ded> wrote:
"David White" <no@email.provi ded> wrote in message news:Sf******* ***********@nas al.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...
Regards
RVG...
|
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 reduced code example:
class List {
private:
void *rootNode;
class Iterator {
|
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 private
members of nesting class.
template <typename T>
class Container {
// NO friendship given to any other
public:
class ContainerIterator;
|
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 group. i want to use a array object in the class but i don't
know how..i am just learning the c++. So i dont know how to use class. in
fact, i have...
|
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 ) {cout<<"a.value="<<a.value<<endl; }
private:
int value;
| |
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 istream &operator>>(istream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
|
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 the individual elements in an
array of ImageList's could be identified by the ID, thereby allowing
re-ordering the array without harm. A person could...
|
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 dog's members can access all
the cat's private members.
// cat.h
//
|
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: 'base::~base' : cannot access protected member declared in
|
by: dolphin |
last post by:
Hello everyone! Can a static member function access non-static member?
I think it is illegal.Is it right?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |