infinite loop ?

Hi,
I can not run this program since yesterday.
What I do wrong?

//a.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#ifndef _A_H_
#define _A_H_

class B;

using namespace std;
class A {
public:
	B *b;
	int i;
	A(int ii);
	A(const A &aa);
	~A();
	void display1()const;
};

#endif




//b.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#ifndef _B_H_
#define _B_H_


#include <iostream>

using namespace std;

class A;

class B{
public:
	A *a;
	int i;
	B(int ii);
	B(const B &bb);
	~B();
	void display2()const;

};




//a.cpp

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
#include "a.h"
#include "b.h"

#include <iostream>


using namespace std;

	
	A::A(int ii)
	{
		i = ii;
		b = new B(23);
		cout << "I AM a`S CONSTURCTOR" << endl;
	}
	A::A(const A &aa)
	{
		i = aa.i;
		b = aa.b;
	}

	A::~A()
	{
		delete b;
	}

	void A::display1()const
	{
		cout << i << endl;
		b->display2();
		cout <<"cikti aldim CLASS A"<< endl;
	}




//b.cpp

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 "b.h"
#include "a.h"

#include <iostream>


using namespace std;



	B::B(int ii)
	{
		i = ii;
		a = new A(4);
		cout << "I AM b`S CONSTURCTOR" << endl;
	}

	B::B(const B &bb)
	{
		i = bb.i;
		a = bb.a;
	}

	B::~B()
	{
		delete a;
	}

	
	void B::display2()const
	{
		cout << i << endl;
		a->display1();
		cout <<"cikti aldim CLASS B"<< endl;
	}






//main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	A a(54);
	B b(23);
	


	a.display1();
	b.display2();
	
	return 0;		
}
Infinite constructor loop by the looks of it.
A creates a B in it's constructor. B creates an A in it's constructor.

So A creates a B which creates an A which creates a B which creates an A which..........


You also have an infinite Display loop.
Last edited on
@guestgulkan

I know it already but , I want to do the following;

1
2
3
4
5
6
class A {
public:
	B *b;
	int i;
};


1
2
3
4
5
6
7
class B{
public:
	A *a;
	int i;
};




class A depends on class B, and class B depends on class A.

Do not have a way of doing this?
Well, you should use a forward declaration:
1
2
3
4
5
6
7
8
9
class B;

class A {
 ...
};

class B {
 ...
};
I have changed, but
now also gives these errors.



error C2011: 'A' : 'class' type redefinition
error C2011: 'B' : 'class' type redefinition
A fatal hug (you can't have 1 to 1)
One of the two constructor should must be changed.
1
2
3
4
5
6
B::B() {
  a = new A( this );
}
A::A(B *_b){
  b = _b;
}
Also check the copy and the destructor
Last edited on
That was what I going to suggest (as in ne555 post): make one of the classes responsible for creating the other but not both like you were doing in the original post.
thanks everyone....
I could easily be wrong, but I think I remember a post stating it's not a good idea to use using namespace std; in a header file. I don't remember why.
I could easily be wrong, but I think I remember a post stating it's not a good idea to use using namespace std; in a header file. I don't remember why.
@HooklessFastener

Why is wrong 'using std namesapace' use?
Because all the files that include that header will be using namespace std.
This could cause conflicts with functions in others namespaces.
(Basically you are killing the reason of use a namespace at all.)
How?
I did not understand exactly.
Suppose that you create a class vector (like in maths or physics). That class will support operations like + - * between vector or vector and scalar, and a lot of other methods. Note that vector is a proper name for that class.
To avoid collisions you put that class in the namespace Math.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Math.h"
#include "header_with_using_namespace_std.h"
#include <vector>
//others headers
using Math::vector; //Is going to be used a lot

typedef std::vector<int> array_int; //to avoid confusion

int main(){
//sorry about the names, but that is not important (you could have an array and a math vector in the same source)
  vector<4> rot;
  array_int arr;
  return 0;
}

The compiler yields
reference to 'vector' is ambiguous
candidates are: template<unsigned int n> class Math::vector
/usr/include/c++/4.4/bits/stl_vector.h:170: error:                 template<class _Tp, class _Alloc> class std::vector
'rot' was not declared in this scope
So now I will have to change all the declarations of vector<n> to Math::vector<n>
as a result:

it's not a good idea to use using namespace std; in a header file.


Topic archived. No new replies allowed.