Circle of pointers

I have a class which contains references to other classes by storing their pointers as variables. This all works great with the exception of one class. One of them needs to also know about the container class so that it can access other contained objects so I tried creating a member variable for this and passing it the this pointer when the container class constructed it. I do have a work around for this but this is how I would prefer to do it if possible.

The specific error is as follows:

"Expected ')' before '*' for both the constructor and the member variable declaration in the header file. No error in the source file. I checked the syntax with other classes and data types and they don't complain which leads me to think that the compiler is complaining about the circular reference. Am I correct in thinking this?

Thanks,

Jared
Last edited on
Post code.
The member class:

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

#include "containerClass.h"

class thisClass
{
public:

	thisClass(containerClass *);
	~thisClass();

protected:

	containerClass *containerPointer
};



Source:
1
2
3
4
5
6
7
8
9
10
11
#include "containerClass.h"
#include "thisClass.h"

using namespace std;

thisClass::thisClass(containerClass *input)
{
	containerPointer = input;
	if (input == NULL)
		perror("Pointer input is NULL");
}


And the containerClass header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

#include "thisClass.h"

class containerClass
{
public:

	containerClass();
	~containerClass();
	
protected:

	thisClass *thisClassPointer;
};
Last edited on
This one's tricky, I'd have to play around with it myself to know for sure, but here is a suggestion based on postings in another thread:

You might be able to add forward declarations for the classes that are used at the top of the header files and only include the other header files in the source files.
Ok, I hope this helps; the following compiles:

A.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef A_H
#define A_H

// forward declaration
class B;

class A
{
    B * _b;
public:
    A();
    A( B * b );
};

#endif /* A_H */ 


A.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "A.hpp"
#include "B.hpp"
    
A::A()
{
}

A::A( B * b ):
    _b( b )
{
}


B.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef B_H
#define B_H

// forward declaration
class A;

class B
{
    A * _a;
public:
    B();
    B( A * a );
};

#endif /* B_H */ 


B.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "B.hpp"
#include "A.hpp"
    
B::B()
{
}

B::B( A * a ):
    _a( a )
{
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "A.hpp"
#include "B.hpp"

int main( int argc, char * args[] )
{
    A a1;
    B b1( &a1 );
    A a2( &b1 );

    return 0;
}


Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
main:    A.o B.o main.o
    g++ A.o B.o main.o -o main

main.o:    A.hpp B.hpp main.cpp
    g++ -c main.cpp

A.o:    A.hpp A.cpp
    g++ -c A.cpp

B.o:    B.hpp B.cpp
    g++ -c B.cpp

clean:
    RM A.o B.o main.o main.exe

Last edited on
Alright I'll give it a shot and repost with results. Thanks for help.

Jared
Seymore I owe you one. That just saved me having to redo a lot of the structure of my program. It worked beautifully (although I don't directly use makefiles, my IDE's handle that bit for me).

Thanks again.

Jared
Cool. Have a good one.
Topic archived. No new replies allowed.