Strange C3767 candidate function(s) not accessible error message
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 ManagedCpperror 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.
精彩评论