1
votes

Can anyone explain why I get the following errors 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;
}
1
It helps to fix error messages from the top down. Earlier messages ("what's public doing after static, it can't go there") often cause later ones ("the function I'm trying to call is private"). - Ben Voigt

1 Answers

3
votes

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.