Using namespaces across multiple files.

The point of this program was to create two objects of type classa and classb which take an address to each others locations and allow access using pointers. I tried spreading this idea across many files and there are many problems with errors. I assume my code is totally wrong but the concept should be possible to implement since I've done this already using a single file with namespaces.

This is the main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "classa.h"
#include "classb.h"

using namespace std;

int main()
{
    classa* a = new classa(99);
    classb* b = new classb(0);

    a.link(b);
    b.link(a);

    delete b;
    delete a;

    return 0;
}


This is the classa.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
25
26
27
#define CLASSA_H ;
#include "namespaceb.h"
#include "namespacea.h"
using namespace CLASSB;
using namespace CLASSA;

class CLASSA::classa
{
   private:

   CLASSB::classb* objb;
   int data;

   public:

   CLASSA::classa(){}
   CLASSA::classa(int value)
   {
       data = value;
   }
   ~classa(){}
   void link(CLASSB::classb* &temp);
};
void CLASSA::classa::link(CLASSB::classb* &temp)
{
    objb = temp;
}


This is the classb.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
25
26
27
28
#define CLASSB_H ;

#include "namespaceb.h"
#include "namespacea.h"
using namespace CLASSB;
using namespace CLASSA;

class CLASSB::classb
{
   private:

   CLASSA::classa* obja;
   int data;

   public:

   CLASSB::classb(){}
   CLASSB::classb(int value)
   {
       data = value;
   }
   ~CLASSB::classb(){}
   void link(CLASSA::classa* &temp);
};
void CLASSB::classb::link(CLASSA::classa* &temp)
{
    obja = temp;
}


This is the namespacea.h
1
2
3
4
namespace CLASSA
{
    class classa;
}


This is the namespaceb.h
1
2
3
4
namespace CLASSB
{
    class classb;
}


closed account (3pj6b7Xj)
since they booth have different names, the namespaces were not needed I think. Unless both classes were called class_clone but they one did something different than the other, then you would use a namespace. I'm not sure if thats what your implying anyway, my eyes went cross-eyed when I looked at the code, hope someone helps you out.
since they booth have different names, the namespaces were not needed I think. Unless both classes were called class_clone but they one did something different than the other,


Well, eventually classa and classb would have different methods so they aren't the same. This idea does work for a single file, let me know if you want me to post that. What I want are two separate files for each class since its easier to work with; but I guess if these are the problems I'm running into, it might be easier to stick to a single file and use that.


EDIT:: I created a single file program of what I want, how would I put this program into 2 header files for each class?
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
#include <iostream>

using namespace std;

class b;

class a
{
    private:
        b* objb;
        int data;

    public:

    void link(b* temp, int value);
    int return_data();
};
class b
{
    private:
        a* obja;
        int data;

    public:
    void link(a* temp, int value);
    int return_data();
};
void a::link(b* temp, int value)
{
    objb = temp;
    data = value;
}
void b::link(a* temp, int value)
{
    obja = temp;
    data = value;
}
int b::return_data()
{
    return data;
}
int a::return_data()
{
    return data;
}
int main()
{
    a* obja = new a;
    b* objb = new b;

    obja->link(objb,99);
    objb->link(obja,0);


    cout<<obja->return_data()<<endl;
    cout<<objb->return_data()<<endl;
    delete objb;
    delete obja;

    return 0;
}


EDIT: I solved the problem for the above code. I guess this thread is solved.
I used this page to work out the solution:

http://efreedom.com/Question/1-1885471/CPlusPlus-Forward-Declaration-Class-Seem-Work

Heres the actual solutions and there are no namespaces to make things simpler.

This is main.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
#include <iostream>
#include "classa.h"
#include "classb.h"

using namespace std;
class a;
class b;

int main()
{
    a* obja = new a;
    b* objb = new b;

    obja->link(objb,99);
    objb->link(obja,0);


    cout<<obja->return_data()<<endl;
    cout<<objb->return_data()<<endl;
    delete objb;
    delete obja;

    return 0;
}


This is classa.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
25
26
27
28
#ifndef CLASS_A
#define CLASS_A

#include "classb.h"
class b;

class a
{
    private:
        b* objb;
        int data;

    public:

    void link(b* temp, int value);
    int return_data();
};
void a::link(b* temp, int value)
{
    objb = temp;
    data = value;
}
int a::return_data()
{
    return data;
}
#endif


and this is classb.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
25
26
27
28

#ifndef CLASS_B
#define CLASS_B

#include "classa.h"
class a;

class b
{
    private:
        a* obja;
        int data;

    public:
    void link(a* temp, int value);
    int return_data();
};
void b::link(a* temp, int value)
{
    obja = temp;
    data = value;
}
int b::return_data()
{
    return data;
}
#endif
Last edited on
Topic archived. No new replies allowed.