Need help to understand the error messege

compiling the code I got the following errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
using namespace std;
class a 	{
protected: int a=5;
	public:
	a(){cout<<"a c"<<endl;}
	};
class b {
protected: int b=6;
	public:
b(){cout<<"b c"<<endl;}
	};

class d	:protected a,protected b
{
	public:
	void display()
	{cout<<a<<b<<endl;}
	d(){cout<<"d c"<<endl;}
};

int main()
{d m;
	m.display();return 0;}


errors:


/root/newtest/main.cpp|4|error: field ‘int a::a’ with same name as class [-fpermissive]|
/root/newtest/main.cpp|9|error: field ‘int b::b’ with same name as class [-fpermissive]|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


why these error are showing
Your compiler doesn't like classes with member variables having the same name.

Do not have a class called a, containing a variable called a.
Do not have a class called b, containing a variable called b.
Hello samir1996,

When I write a class I like to use this for the variable names: "m_variableName". Where the "m_" says the this is a member variable of a class. This way if you try to use this kind of variable outside of a class member function it will help understand the compiler error.

Your code would read much better with the proper indentation and a couple of blank lines. In class a, d and main the {}s are hard to find. In your code for main I almost missed finding them. The following code makes the code much easier to read and follow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;  // <--- Best not to use

class a
{
	protected: int m_a = 5;

	public:
		a() { cout << "a c" << endl; }
};

class b
{
	protected: int m_b = 6;

	public:
		b() { cout << "b c" << endl; }
};

class d :protected a, protected b
{
	public:
		void display()
		{
			cout << a << b << endl;
		}
		d() { cout << "d c" << endl; }
};

int main()
{
	d m;
	m.display();

        return 0;
}


Hope that helps,

Andy
Thanks a lot ,both Andy and repeater.This helped me a lot
Topic archived. No new replies allowed.