Visual C ++ / CLI两个类对象之间的相互访问?

时间:2010-10-15 10:10:06

标签: visual-c++ class namespaces c++-cli

问题在于:

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();
};

我得到了错误:

该行是:

'private: RunnableThread^ peerThread;'

然后错误是:

  

错误C2146:语法错误:丢失   ';'在标识符之前   'peerThread'k:\ visual studio   2010 \项目\程序1 \程序1 \ Form1.h   &LT; ....&GT;


看来,

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

 } }

但是我也可以在'命名空间Program1'之前移动'RunnableThread'声明代码,因为'RunnableThread'使用指向父'Form1'的指针,他创建了这个类的实例。

如何解决这个问题?

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

class Form1之前添加转发声明:

class RunnableThread;

可能前面有ref