Visual C++/CLI mutual access between two class objects? – Education Career Blog

Here is the problem:

namespace Program1 {
   public ref class Form1 : public System::Windows::Forms::Form
   {
    public: Form1(void) {....}
         private: RunnableThread^ peerThread;

   private: System::Void loginButton_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
    String^ ip = this->ipTextField->Text;
    String^ port = this->portTextField->Text;
        <.............>
        // Start new thread
        this->peerThread = gcnew RunnableThread("thread2", ip, port, this->gameMatrix, this);
        <..............>
    }
}

   }


// Runnable class
ref class RunnableThread
{
private:
    String^ ip;
    String^ port;
    <...>
    EchoClient3WS::Form1^ refToRootObj;
    <......>
public:
    RunnableThread(String^ threadName, String^ ip, String^ port, GameMatrix^ gameMatrix, Program1::Form1^ rootObj);
    void run();
    void callServer(String^ message);
    void done();
};

And I got error:

The line is:

'private: RunnableThread^ peerThread;'

Then error is:

error C2146: syntax error : missing
‘;’ before identifier
‘peerThread’ k:\visual studio
2010\projects\program1\program1\Form1.h
<….>


It seems, that

namespace Program1 { public ref class Form: <...> {   
// HERE WE DON'T KNOW ANYTHING ABOUT THE CLASS NAMED 'RunnableThread'

 } }

But I also can move the ‘RunnableThread’ declaration code before the ‘namespace Program1’, because the ‘RunnableThread’ uses pointer to father ‘Form1’ , who created an instance of this class.

How to solve this problem?

Thanks for any answer.

,

Add a forward declaration before class Form1:

class RunnableThread;

probably with the ref in front.

Leave a Comment