如何在 C++/CLI 中转发声明属性?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将实现更改为:
Change the implementation to: