如何在 C++/CLI 中转发声明属性?

发布于 2024-08-02 22:42:35 字数 927 浏览 8 评论 0原文

我有一个 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'

任何人都可以阐明这个问题吗?

I have a class in C++/CLI that I'd like to give a property. I want to declare the property in a header file and then implement that property in a .cpp file.

Here's the header:

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

If I declare the header file and don't put anything in the cpp, i get the following error:

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

From this I concluded that I should implement the property as

  static Lock myInstanceLock;

   Dude^ Dude::get_instance()
   {

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

However, when I compile this code, I get a bunch of errors. The first error (The others are a result of the first one) is:

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

Can anyone shed some light on this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2024-08-09 22:42:35

将实现更改为:

Dude^ Dude::instance::get()

Change the implementation to:

Dude^ Dude::instance::get()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文