rc\main.cpp:16:10: error: 'class A' has no member named 'p' (a.p).If I create object and access its member(a.z) then program runs with no errors. The difference is that if I create object and accessed any members being initialized, it throws an error "...no member.." above. What could be wrong about this?
I'm sorry for the unformatted post the code parameters were not working.
#include<iostream>
#include"start_it.h"
using namespace std;
int main()
{
A a;
cout<<a.p;
return 0;
};
.h
start_it.h
#ifndef START_IT_H_
#define START_IT_H_
class A
{
private:
int x = 45;
public:
int z = 67;
A(){//constructor
int o = 58;
int p = 67;
int q = 130;
int z = 200;
};
~A(){}; //destructor*/
A class has only two members: x and z.
In constructor you are declaring several local variables. BTW, z in constructor body is not the same z as class member and changes to it will not be reflected on member.
Follow up: I made the initialization list variables class members - thankyou! That seemed to work well. My remaining question ( a little test I did): I added two identical members to public and private both member variables (A.z1). I got an error but from the private class not the public. Is this the usual order?
start_it.h
#ifndef START_IT_H_
#define START_IT_H_
class A
{
private:
int z1=500;
int y = 45;
public:
int z1 =250;
int o = 58;
int p = 67;
int q = 130;
A(){//constructor
int z1;
int o = 58;
int p = 78;
int q = 130;
int z = 200;
};
~A(){}; //destructor*/
};
#include <iostream>
#include "start_it.h"
#endif /* START_IT_H_ */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
#include"start_it.h"
usingnamespace std;
int main()
{
A a;
cout<<a.z1;
return 0;
};
Yes, it is expected. THey are both members in same scope. Only difference is who can access them. So they cannot have same name.
BTW, you do not need all the stuff in your constructor as it does nothing anyway. Actually you do not strictly need to define constructor/destructor at all here.
Thank you for your help so far. As a "purist" exercise I need one more thing cleared up. When I initialize the members values with different values it doesn't change them. E.g.
1 2 3 4 5 6 7 8
private:
//int z1=500;
int y = 45;
public:
int z1 =250;
int o = 58;
int p = 67;
int q = 130;
Constructor called vars:
1 2 3 4
int o = 98;
int p = 78;
int q = 430;
int z = 200;
1 2 3 4 5
public:
int z1 =250;
int o = 58;
int p = 67;
int q = 130;
Stays the same ...
The initialization vars don't replace the member values.
Shouldn't the public vars be changed to initialization list?
In the constructor, you are declaring new local variables with the same names as your fields. Don't declare new variables; just assign to them as the fields have already been defined.
1 2
int x = 0; // declares & assigns to new variable x
x = 0; // assigns to existing variable x
That worked like a charm. Thank you. Do I need to also include functions in the initialization list as well? I would think this would remain of the implementation side?
No, because functions are not variables and do not need to be initialized.
I'm not sure if you've changed this, but why are you including "start_it.h" inside of itself? Also, including <iostream> at the end of start_it.h does not make much sense to me either.
Nor me. Doesn't make sense. Those don't belong here. Its something the IDE is doing sometimes after compilation. The main.cpp has it correct though. I'll have to keep a watch on those unless u have some advice on that.