1

当我有这个 c++ cli 代码时:

#pragma managed
ref class mcSessions: ConcurrentDictionary <String ^, mcSession^> 
{  //<---- warning here
private:
   static mcSessions ^g_Sessions;
       TimeSpan MaxIdleTime;
   mcSessions (TimeSpan newMaxIdleTime);
public:
    static void Initialize (long MaxIdleMinutes);
        static void Shutdown ();
        static void CreateSession (String ^&SessionId);
        static void RunInSession (String ^SessionId, String ^Command);
        static void RemoveSession (String ^SessionId);
};

我在第一个 { 上收到警告 C4538。有没有人知道为什么会发生这种情况以及它警告什么,或者这也是一个编译器错误,因为它是针对这个问题中的情况:使用 C++/CLI 的看似不恰当的编译警告

尽管我看到它们有些相关,但我没有Group对象而是ConcurrentDictionary. 我正在使用针对 .NET 4.0 框架的 Visual Studio 2010 并使用 /clr 编译器标志进行编译。

确切的错误信息是:

1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=System::Collections::Concurrent::ConcurrentDictionary::Node ^,
1>              dimension=1
1>          ]
1>          This diagnostic occurred while importing type 'System::Collections::Concurrent::ConcurrentDictionary ' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=int,
1>              dimension=1
1>          ]
1>          This diagnostic occurred while importing type 'System::Collections::Concurrent::ConcurrentDictionary ' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=System::Collections::Concurrent::ConcurrentDictionary<TKey,TValue>::Node ^,
1>              dimension=1
1>          ]
1>          d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25) : see reference to class generic instantiation 'System::Collections::Concurrent::ConcurrentDictionary<TKey,TValue>' being compiled
1>          d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25) : see reference to class generic instantiation 'System::Collections::Concurrent::ConcurrentDictionary<TKey,TValue>' being compiled
1>          with
1>          [
1>              TKey=System::String ^,
1>              TValue=IamPowershell::mcSession ^
1>          ]
1>          This diagnostic occurred while importing type 'System::Collections::Concurrent::ConcurrentDictionary::Node ' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=int,
1>              dimension=1
1>          ]
1>          This diagnostic occurred while importing type 'System::Collections::Concurrent::ConcurrentDictionary::Node ' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=System::Collections::Concurrent::ConcurrentDictionary<System::String ^,mcSession ^>::Node ^,
1>              dimension=1
1>          ]
1>d:\t4edevnet2010\umra2\iampowershell\mcIpSessions.h(25): warning C4538: 'cli::array<Type,dimension> ^' : const/volatile qualifiers on this type are not supported
1>          with
1>          [
1>              Type=int,
1>              dimension=1
1>          ]
1>
4

2 回答 2

1

如前所述,问题在于 ConcurrentDictionary 中的易失性成员。但是,从您显示的代码中,您不需要从 ConcurrentDictionary 继承。

一般来说,我相信如果你正在制作一个通用的、可重用的集合类,你只需要从集合类继承。这似乎不是这里的情况,它似乎非常特定于mcSession类,并且可能具有业务逻辑,而不是简单的集合逻辑。

如果你这样声明你的班级,它会起作用吗?

ref class mcSessions
{
private:
    static ConcurrentDictionary<String^, mcSession^>^ g_Sessions;
    TimeSpan MaxIdleTime;
    mcSessions (TimeSpan newMaxIdleTime);
public:
    static void Initialize (long MaxIdleMinutes);
    static void Shutdown ();
    static void CreateSession (String ^&SessionId);
    static void RunInSession (String ^SessionId, String ^Command);
    static void RemoveSession (String ^SessionId);
};

编辑:还是这样?

ref class mcSessions
{
private:
    static mcSessions^ g_Sessions;// static session manager
    ConcurrentDictionary<String^, mcSession^>^ SessionList; // instance variable on g_Sessions
    TimeSpan MaxIdleTime;
    mcSessions (TimeSpan newMaxIdleTime);
public:
    static void Initialize (long MaxIdleMinutes);
    static void Shutdown ();
    static void CreateSession (String ^&SessionId);
    static void RunInSession (String ^SessionId, String ^Command);
    static void RemoveSession (String ^SessionId);
};
于 2013-02-07T17:01:12.627 回答
0

正如@HansPassant 在对问题的评论中指出的那样,他是对的。成员确实不稳定(愚蠢的我,可以检查一下:S)无论如何这应该是答案。谢谢,我认为它现在解决了。

于 2013-02-07T14:55:50.500 回答