【问题标题】:Visual C++/CLI mutual access between two class objects?两个类对​​象之间的 Visual C++/CLI 相互访问?
【发布时间】:2010-10-15 10:10:06
【问题描述】:

问题来了:

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\projects\program1\program1\Form1.h <....>


看来,

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

 } }

但我也可以将 'RunnableThread' 声明代码移到 'namespace Program1' 之前,因为 'RunnableThread' 使用指向创建此类实例的父亲 'Form1' 的指针。

如何解决这个问题?

感谢您的任何回答。

【问题讨论】:

  • 这种愚蠢的头痛是许多 C++/CLI 程序员转向 C# 的原因之一。推荐,C++/CLI的winforms编程没有优势,没有前途。

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


【解决方案1】:

class Form1前添加前向声明:

class RunnableThread;

可能前面有ref

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多