开发者

Strange C3767 candidate function(s) not accessible error message

开发者 https://www.devze.com 2023-03-08 20:17 出处:网络
Can anyone explain why I get the following errors开发者_StackOverflow中文版 when compiling the code shown below (and how to fix it)

Can anyone explain why I get the following errors开发者_StackOverflow中文版 when compiling the code shown below (and how to fix it)

error C3767: 'ManagedClass::SetString': candidate function(s) not accessible e:\Temp\ManagedCpp\ManagedCpp\ManagedCpp.cpp 24 ManagedCpp

error C3767: 'ManagedClass::GetString': candidate function(s) not accessible e:\Temp\ManagedCpp\ManagedCpp\ManagedCpp.cpp 26 ManagedCpp

I read the following similar question, C++ CLI error C3767: candidate function(s) not accessible which states

I recommend using the managed type System::String^ instead in all your public API. This also ensures that your library is easily callable from other CLR languages such as c#

Which is exactly what I did (BTW This is a test code used to extract the same compilation error in a much larger mixed mode dll).

(The project is a VS2008 C++/CLI project i.e from Menu select File->New Project->Visual C++->CLR Console Application.)

Thanks for all you help.

using namespace System;

static public ref class ManagedClass
{
    static public int SetString(String^ s)
    {
        str = s;
    }

    static public String^ GetString()
    {
        return str;
    }

    static String^ str ;
};

int main(array<System::String ^> ^args)
{
    String^ test ="Here";
    ManagedClass::SetString(test);
    String^ j=  ManagedClass::GetString();
    return 0;
}


You're using C#-ish syntax; the proper C++/CLI syntax is:

public ref class ManagedClass abstract sealed
{
public:
    static void SetString(String^ s) { str = s; }
    static String^ GetString() { return str; }

private: // I assume you want this even though your code omitted it
    static String^ str;
};

Note that it would be more idiomatic for .NET code to use a property rather than a get/set member-function pair.

0

精彩评论

暂无评论...
验证码 换一张
取 消