Error in the main to call the function !

i get this erorr today when im trying to use the inhertance with class , and I was surprised by this error in the main say

1- error C2374: 'D' : redefinition; multiple initialization

2- see declaration of 'D'

but when i remove the D things get worse and complex

THE ERORR IN THE LINE 60

the program :

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# include <iostream>
using namespace std;
class base
{
private:
	int u;
	int v;
public:
	base();
	base(int,int);
	void print();
};
class Derived:public base
{
private:
	float first;
	int second;
public:
	Derived();
	Derived(int,int,float,int);
	void print();
};

base::base()
{u=0;
v=1;
cout << "base";}

void base::print()
{cout << u << " " <<v;}

base::base(int a, int b)
{
	u=a;
	v=b;
}

Derived::Derived()
{
	first=10;
	second=15;
}

void Derived::print()
{
	base::print();
	cout << first << "  " << second << endl;
}



void main()
{
	base B;
	B.print();
	base (2,3);
	B.print();
	Derived D;                 
	D.print();
	Derived D(3,4,5,6);          //here is the error 
	D.print();
}
Last edited on
EDIT: You edited your post. Why do you not understand the error? You are defining two different variables with the same name.
1
2
3
4
5
6
7
8
9
10
11
void main() //error: should be int main()
{
	base B;
	B.print();
	base (2,3);
	B.print();
	Derived D; //here is the error 
	D.print();
	Derived D(3,4,5,6); //the error is actually here
	D.print();
}


Your other problem is that you need to make your destructors virtual, and your print functions need to be virtual, among other things wrong with your code.
Last edited on
Yes Thanks for the corrective
Why don't you understand the error? You're defining two different variables with the same name "D".
we cloudent say int main()
becuase we have functions does not return any thing
void main() is illegal C++ syntax. The standard mandates that main must return an int.

In C++, if there is no return statement in main, 0 is automatically returned. This is a special case for the main function.
Last edited on
and what is the correction in the code
before of that i said when i remove the D things get worse and complex
which means i know i have tow variables with same name but when i remove it i have another complex erorr !
Think about what it means to have two variables with the same name. Answer: just give them different names.
closed account (zb0S216C)
alsatrawi wrote:
" when i remove it i have another complex erorr !"

...and what's the error?

Wazzak
This is the error:
error: ‘::main’ must return ‘int’
As you can see, it is extremely complex.
Last edited on
Topic archived. No new replies allowed.