【问题标题】:How do I Forward Declare a Property in C++/CLI?如何在 C++/CLI 中转发声明属性?
【发布时间】:2009-08-24 15:04:38
【问题描述】:

我想提供一个属性的 C++/CLI 类。我想在头文件中声明该属性,然后在 .cpp 文件中实现该属性。

这是标题:

public ref class Dude
{
    static property Dude^ instance
    {
        Dude^ get();    
    }
}

如果我声明头文件并且没有在 cpp 中添加任何内容,我会收到以下错误:

1>Dude.obj : error LNK2020: unresolved token (06000001) Test.Dude::get_instance

由此我得出结论,我应该将该属性实现为

  static Lock myInstanceLock;

   Dude^ Dude::get_instance()
   {

       if(myInstance == nullptr)
       {
           myInstanceLock.lock();
           if(myInstance == nullptr)
           {
               myInstance = gcnew Dude();
           }
           myInstanceLock.unlock();             
       }
       return myInstance;
   }

但是,当我编译这段代码时,我得到了一堆错误。第一个错误(其他是第一个错误的结果)是:

1>.\Dude.cpp(13) : error C2039: 'get_instance' : is not a member of 'Test::Dude'

谁能解释一下这个问题?

【问题讨论】:

    标签: c++-cli properties forward-declaration


    【解决方案1】:

    将实现更改为:

    Dude^ Dude::instance::get()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-04
      • 2014-05-17
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多