2017-07-19 466 views
1

我在處理從C#應用程序接收到的字符串^的數組時出現問題。爲什麼我不能創建一個String ^數組? 我對C++相當陌生,所以不勝感激。不允許使用數組的句柄C++

public ref class Example 
    { 
     public: 
      String^ Convert(String^ pointNames[], String^ outputPath) 
      { 

       std::string convertedPath = msclr::interop::marshal_as<std::string>(outputPath); 
       std::string result = otherFunction(pointNames, convertedPath); 

       return msclr::interop::marshal_as<String^>(result); 
      } 
    }; 

pointsNames []帶下劃線作爲錯誤,消息:不允許使用數組句柄。

將一個字符串數組從C#應用程序發送到C++會更好嗎?

+0

快速搜索建議將'String^pointNames []'改爲'WriteOnlyArray ^pointNames' - 你可以試試嗎? – Thebluefish

+0

@Thebluefish我不知道我明白了,我用WriteOnlyArray替換了String^ ^但編譯器只是說WriteOnlyArray不是一個模板,我必須包含一些庫嗎? –

+0

它看起來像屬於「平臺」命名空間 - 取自[本文](https://social.msdn.microsoft.com/Forums/en-US/f0e968b2-5798-430a-8a00-d45c4a9ef1e0/pointer-對AC-陣列的帽型手柄?論壇= winappswithnativecode)。 – Thebluefish

回答

1

你試圖在那裏聲明一個非託管數組類型,但是你需要一個託管數組來保存管理類型。

聲明參數爲array<String^>^ pointNames

注:這是std::array,這是cli::array,但/clr然後using namespace cli;編譯時被暗示。

+0

那麼我會如何將一個cli :: array作爲一個字符串[]呢? –

+0

Threre沒有像'marshal_as'這樣的內置方法,你只需要編寫一個循環並逐一轉換每個字符串。 –