2010-01-27 53 views
2

在C++中,我有一個char [256]變量,通過調用一個外部DLL來填充數據。從那裏我想添加char []作爲ComboBox項目。將char []轉換爲System :: Object

char name[256]; 
name[0] = "76"; 
comboBox1->Items->Add(name); 

由於char []不是System :: Object的類型,所以會產生構建錯誤。任何關於將char []轉換爲我可以將其作爲Item添加到ComboBox控件的想法?轉換爲字符串會很好,但我不確定如何做到這一點。此外,如果我嘗試創建一個變量,例如:

string strName; 

也會爲缺少的';'創建一個錯誤。在標識符'strName'之前。我是C++的初學者,並且仍然讓我的大腦纏繞它,所以感謝您提供的任何幫助!

編輯 所要求的完整代碼:

#pragma once 

命名空間FMODMultipleSoundcardWindowed {

#include <string> 
#include "inc/fmod.h" 
#include "inc/fmod_errors.h" 

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 
using namespace std; 


FMOD_SYSTEM  *systemA, *systemB; 
FMOD_SOUND  *soundA, *soundB; 
FMOD_CHANNEL *channelA = 0, *channelB = 0; 
FMOD_DSP  *dspNormalizerA, *dspNormalizerB; 
FMOD_DSP  *dspCompressorA, *dspCompressorB; 
FMOD_DSP  *dspEqualizerA[10], *dspEqualizerB[10]; 
FMOD_DSP  *dspVSTVUA, *dspVSTVUB; 
FMOD_RESULT  result; 
unsigned int dspVSTVUHandleA, dspVSTVUHandleB; 
unsigned int version; 
int    numdrivers, count; 


public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) 
    { 
     InitializeComponent(); 

     result = FMOD_System_Create(&systemA); 
     ERRCHECK(result); 

     result = FMOD_System_GetVersion(systemA, &version); 
     ERRCHECK(result); 

     if (version < FMOD_VERSION) 
     { 
      MessageBox::Show("You are using an old version of FMOD!"); 
     } 

     result = FMOD_System_GetNumDrivers(systemA, &numdrivers); 
     ERRCHECK(result); 

     for (count = 0; count < numdrivers; count++) 
     { 
      char name[256]; 

      result = FMOD_System_GetDriverInfo(systemA, count, name, 256, 0); 
      ERRCHECK(result); 

      m_objPrimaryAudioDeviceComboBox->Items->Add(name[0]); 
     } 
    } 

    void ERRCHECK(FMOD_RESULT result) 
    { 
     if (result != FMOD_OK) 
     { 
      MessageBox::Show("FMOD Error!"); 
      this->Close(); 
     } 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() 
    { 
     if (components) 
     { 
      delete components; 
     } 
    } 
private: System::Windows::Forms::ComboBox^ m_objPrimaryAudioDeviceComboBox; 
protected: 
private: System::Windows::Forms::ComboBox^ m_objSecondaryAudioDeviceComboBox; 

private: 
    System::ComponentModel::Container ^components; 

編譯區域Windows窗體設計器生成的代碼

void InitializeComponent(void) 
    { 
     this->m_objPrimaryAudioDeviceComboBox = (gcnew System::Windows::Forms::ComboBox()); 
     this->m_objSecondaryAudioDeviceComboBox = (gcnew System::Windows::Forms::ComboBox()); 
     this->SuspendLayout(); 
     // 
     // m_objPrimaryAudioDeviceComboBox 
     // 
     this->m_objPrimaryAudioDeviceComboBox->FormattingEnabled = true; 
     this->m_objPrimaryAudioDeviceComboBox->Location = System::Drawing::Point(12, 12); 
     this->m_objPrimaryAudioDeviceComboBox->Name = L"m_objPrimaryAudioDeviceComboBox"; 
     this->m_objPrimaryAudioDeviceComboBox->Size = System::Drawing::Size(254, 21); 
     this->m_objPrimaryAudioDeviceComboBox->TabIndex = 0; 
     // 
     // m_objSecondaryAudioDeviceComboBox 
     // 
     this->m_objSecondaryAudioDeviceComboBox->FormattingEnabled = true; 
     this->m_objSecondaryAudioDeviceComboBox->Location = System::Drawing::Point(12, 54); 
     this->m_objSecondaryAudioDeviceComboBox->Name = L"m_objSecondaryAudioDeviceComboBox"; 
     this->m_objSecondaryAudioDeviceComboBox->Size = System::Drawing::Size(254, 21); 
     this->m_objSecondaryAudioDeviceComboBox->TabIndex = 1; 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(440, 426); 
     this->Controls->Add(this->m_objSecondaryAudioDeviceComboBox); 
     this->Controls->Add(this->m_objPrimaryAudioDeviceComboBox); 
     this->Name = L"Form1"; 
     this->Text = L"FMOD Multiple Soundcard with VST"; 
     this->ResumeLayout(false); 

    } 

編譯連接DREGION

}; 

}

+0

附錄給我的回答和下面的註釋:把'include '行放在'namespace FMODMultipleSoundcardWindowed {'之外。否則,'std'結束爲'FMODMultipleSoundcardWindowed :: std'。 – 2010-01-27 17:15:01

+0

這讓我能夠按照計劃訪問字符串對象,這非常棒。但我原來的問題存在,我不能添加一個std:字符串作爲組合框項,因爲'不能將參數1從'std :: string'轉換爲'System :: Object ^' – Jeff 2010-01-27 17:23:42

+0

編輯我的答案。 – 2010-01-27 17:27:04

回答

1
string strName; 

首先,你需要#include <string>才能使用std::string。其次,您需要將using namespace std;添加到您的設備中,以便將std::string設爲string。或者,也可以使用簡單的std::string strName。這是一個品味問題。使用完全限定的格式可避免使用來自std的所有標識符污染您的名稱空間。

與主要問題:

gcnew String(text.c_str()); 

這應該做的轉換。 text是您的std::string實例,表達式的結果是您需要傳遞給Add的字符串對象。

+0

我正在VS2005與我的C++項目。添加對std的任何引用都會返回'std':不是類或名稱空間名稱。我在這裏錯過的東西? – Jeff 2010-01-27 16:54:57

+0

如果你在'using namespace std;'之前使用'#include ',它應該可用。 – 2010-01-27 16:57:00

+0

當我嘗試這個構建從文件cstdio和cstdlib拋出100多個錯誤。 – Jeff 2010-01-27 16:59:38

0

嘗試string strName(name);(你有你的包括和命名空間權的假設......)

更新:它看起來像您需要使用System::String,而不是STL字符串。

+0

注意。但我想我錯過了一些包含/名稱空間。再次 - 我仍然在學習和弄清楚這些東西。 C++和字符串打擊了我的想法,因爲在VB和C#中使用任何我想要的東西都很容易。 – Jeff 2010-01-27 16:59:19

+0

我的歉意:)正如其他答案指出的那樣,你應該可以'#include '然後使用'使用命名空間標準;'或者改變你的調用來使用'std :: string'而不是'string'。看看這個:http://www.devarticles.com/c/a/Cplusplus/The-STL-String-Class/ – acron 2010-01-27 17:27:22

0

你的第二個問題聽起來好像你錯過了包括String類,所以只需添加#include <string>到文件的頂部,你可能還需要添加using namespace std;,或者根據您的系統上使用「的std :: string」

+0

當我嘗試這個構建會從文件cstdio和cstdlib中拋出100多個錯誤。我錯過了什麼嗎?順便說一句 - 在VS2005 C++中工作。謝謝你的幫助。 – Jeff 2010-01-27 16:56:27

+0

可能是因爲我在'using namespace std'後面忘了分號:|,在上面修正了 – Necrolis 2010-01-27 17:35:34

相關問題