referenced in function " void *thiscall B::`scalar deleting destructor'

Hi,



a.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 _A_H_
#define _A_H_


#include "b.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
23
24
#pragma once
#ifndef _B_H_
#define _B_H_



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

};
#endif 




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


using namespace std;

	
	A::A(int ii)
	{
		i = ii;
		b = new B(100);
		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
#include "b.h"
#include <iostream>


using namespace std;



	

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

	B::B(const B &bb)
	{
		i = bb.i;
		a = bb.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
13
14
15
16
17
18

int main()
{

	

	B b(23);
	A a(54);


	a.display1();
	b.display2();
	
	return 0;		
}




error is:

anifestResourceCompile:
1> All outputs are up-to-date.
1>a.obj : error LNK2019: unresolved external symbol "public: __thiscall B::~B(void)" (??1B@@QAE@XZ) referenced in function "public: void * __thiscall B::`scalar deleting destructor'(unsigned int)" (??_GB@@QAEPAXI@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall B::~B(void)" (??1B@@QAE@XZ)
1>C:\Users\firix\Desktop\PROJE YENÄ°\ya olacak ya olacak\Debug\ya olacak ya olacak.exe : fatal error LNK1120: 1 unresolved externals
You can't have a.h include "b.h" then have b.h include a.h.

Since you are just using a pointer, just forward declare the class then remove those includes.
I not understand
Would you show it to me?
Remove lines 6/7 in a.h and b.h respectively.
I remove but still the same
Last edited on
You haven't provided an implemention of B's destructor, but you declared it.
I did but it still continues -:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;

};
 

#endif 



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

#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;
	}



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
#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;
	}


 


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


a.display1();
b.display2();
	
return 0;		
}

			
Last edited on
Why main is not including a.h and b.h ?
Last edited on
Do you realize you are infinitely recursing here?

If A's ctor creates a B, and B's ctor creates an A....

then A makes a B which makes an A which makes a B which makes an A which makes a B, etc, etc, etc. It will loop until you run out of stack space and the program explodes.

EDIT: Also your copy constructor is ill formed and your assignment operator is nonexistant. This has potential memory leaks.
Last edited on
http://www.cplusplus.com/forum/beginner/31829/
Lets talk in just one place.
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 &operator=(const A &aa);
	~A();
	void display1()const;
};

#endif 


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
#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 &operator=(const B &bb);
	~B();
	void display2()const;

};
 

#endif



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
#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 = new B(rand() % 100);
	}

	A &A::operator=(const A &aa)
	{
		delete b;
		b = new B(rand() % 100);
		return *this;
	}

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

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



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
#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 = new A(rand() % 100);
	}


	B &B::operator=(const B &bb)
	{
		delete a;
		a = new A(rand() % 100);
		return *this;
	}

	B::~B()
	{

		delete a;
	}

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


 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "a.h"
#include "b.h"


int main()
{
B b(23);
A a(54);
		
	


a.display1();
b.display2();
	
return 0;		
}


I made the changes, but still fails to
Last edited on
What are trying to aside from destroying your computer? If A creates B and B creates A, you're bound to have problems
This is a double post - OP also has this same question going under the title 'infinite loop' in
this very forum -
Last edited on
@sargon94

Do not be such a thing?
I made the changes
No you don't
but still fails to
to... what?
(you are missing #include<cstdlib> so you can use rand )
It compiles fine
 g++ -c *.cpp
g++ *.o -o a.out

And it crashes like it should, so what is the problem?
ahem:

I wrote:
Do you realize you are infinitely recursing here?

If A's ctor creates a B, and B's ctor creates an A....

then A makes a B which makes an A which makes a B which makes an A which makes a B, etc, etc, etc. It will loop until you run out of stack space and the program explodes.


You simply can't do this. It's logically impossible. You're trying to create an infinite amount of objects.
@Disch

http://cplusplus.com/forum/articles/10627/#msg49679

In your article.you have defined..


1
2
3
4
5
6

 // a.h -- assume it's guarded
#include "b.h"

class A { B* b; };
 


1
2
3
4
5
6
 // b.h -- assume it's guarded
#include "a.h"

class B { A* a };
 
yes, you can have pointers to other objects, but you can't have them each create each other.

Forget about the code and think about the logic. If every A has a unique B, and every B has a unique A.... then you have an infinite amount of objects. As soon as you make an A, it makes a B which makes another A which makes another B which makes yet another A which makes yet another B, etc, etc, etc.

What you're trying to do is similar to this... also mentioned in that article:

My article wrote:
1
2
3
4
5
6
7
8
// a.h (guarded)

#include "b.h"

class A
{
  B b;   // B is an object, can't be forward declared
};


1
2
3
4
5
6
7
8
// b.h (guarded)

#include "a.h"

class B
{
  A a;   // A is an object, can't be forward declared
};


You may note, however, that this situation is conceptually impossible. There is a fundamental design flaw. If A has a B object, and B has an A object, then A contains a B, which contains another A, which contains another B, which contains another A, which contains another B, etc, etc. You have an infinite recursion problem, and either class is simply impossible to instantiate.




What you CAN do, is you can have A create a B, and then have B point to an existing A (without creating a new one). So something like this would work:

1
2
3
4
5
6
7
8
9
10
11
12
class A {
 B* b;
 A() {
   b = new B;
};

class B {
  A* a;
  B(A* aa){
    a = aa;
  }
};


Now this is OK because B isn't creating a new A, it's simply using an existing A.
thank everyone..
Topic archived. No new replies allowed.