Hello, I am working on developing an interpreted programming language for a capstone project and have hit a snag dealing with two classes. In the interpreter, the classes represent a scope containing a procedure pointer and the procedure pointing back to the scope containing it. The former to allow the call_procedure ( keytype N ) function to look up procedures and to avoid name conflicts (storing procedure pointers in the scope structure as opposed to a long std::map<keytype, procedure> means multiple procedures can have the same name). The latter allows the procedure to access variables stored in a the scope in which it operates. keytype is a template argument taken by the scope class, which determines what kind of value everything is mapped to; it is presently a std::vector<char> .

As it is, this creates an infinite recursion of types in theory, but in practice just causes the compiler to spit an error at me.

The following testbed code demonstrates the behaviors I would like to have:

class a
{
   public:
      // links to an object of class B
      void link (b * other, bool check)
      {
         test = other;
         if ( !check ) { test -> link (this, 1); }
      }
      // example function
      void say () { std::cout << "I am an A."; }
      // example message-passing function
      void say_other () { test -> say(); }
   private:
      b * test;
};

// acts just like class A, but with different pointers
class b
{
   public:
      void link (a * other, bool check)
      {
         test = other;
         test -> link (this, 1);
      }
      void say () { std::cout << "I am a B."; }
      void say_other () { test -> say(); }
   private:
      a * test;
};

int main ()
{
   // create one of each object
   a alpha;
   b beta;
   
   // give beta to a reference to alpha
   beta.link(&alpha, 0);
   
   // test functions from beta side
   beta.say();
   std::cout << std::endl;
   beta.say_other();
   
   std::cout << std::endl << std::endl;
   
   // test functions from alpha side
   alpha.say();
   std::cout << std::endl;
   alpha.say_other();
}

I am completely aware that the above code does not work; is there some way for me to have two separately-declared and otherwise independent classes contain pointers to each other?

(If I left out any explanation or if more code is needed, I can dredge through my scope and procedure source, but this is really more general than that specific application.)

I'm not sure I understand what you mean with the whole interpreted language application stuff.

But, all you need to make that little snippet of code work is to forward-declare the types. Specifically, you can add the following at the very beginning of the code you posted:

class b; //forward-declaration of class b.

And your code will compile just fine.

I don't know if that solves your real problem, but it answers the question you asked.

commented: Thanks for the forward-declaration aid! +1

I'm not sure I understand what you mean with the whole interpreted language application stuff.

But, all you need to make that little snippet of code work is to forward-declare the types. Specifically, you can add the following at the very beginning of the code you posted:

class b; //forward-declaration of class b.

And your code will compile just fine.

I don't know if that solves your real problem, but it answers the question you asked.

Thanks a lot! It took a few tweaks (I needed to move the functions out of A to avoid another error), but this solved the problem. I build applications in little steps; procedure.h is already compiling as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.