代码之家  ›  专栏  ›  技术社区  ›  Jeff

将char[]转换为System::Object

  •  2
  • Jeff  · 技术社区  · 16 年前

    在C++中,我有一个char(256)变量,它被调用到一个外部DLL,用它填充数据。在此,我想将char[]添加为组合框项。

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

    这将创建生成错误,因为char[]不是System::Object的类型。对于将char[]转换为可作为项添加到组合框控件中的内容,有什么想法吗?转换为字符串是很好的,但我不太确定如何转换。另外,如果我试图创建一个变量,例如:

    string strName;
    

    还为标识符“strName”之前缺少“;”创建错误。我是初学C++的人,我的大脑仍然被包裹着,所以谢谢你提供的任何帮助!

    编辑 要求的完整代码:

    #pragma once
    

    命名空间fmodmultipleoundcardwindowed{

    #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;
    

    pragma region 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);
    
        }
    

    pragma结束区域

    };
    

    }

    3 回复  |  直到 16 年前
        1
  •  1
  •   Alexander Gessler    16 年前
    string strName;
    

    首先,你需要 #include <string> 为了使用 std::string . 其次,您需要添加 using namespace std; 到你的单位,以便参考 STD::字符串 作为 string . 或者,使用平原 std::string strName 相反。这是一个品味问题。使用完全限定的表单可以避免使用来自 std .

    关于主要问题:

    gcnew String( text.c_str() );
    

    这应该可以进行转换。 text 是你的 STD::字符串 实例和表达式的结果是需要传递给的字符串对象 Add .

        2
  •  0
  •   Necrolis    16 年前

    第二个问题听起来像是缺少字符串类的include,所以只需添加 #include <string> 在文件顶部,您可能还需要添加 using namespace std; 或者根据您的系统使用“std::string”

        3
  •  0
  •   Antony Woods    16 年前

    尝试 string strName(name); (假设您的include和名称空间是正确的…)

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