在C ++中,我有一个char [256]变量,该变量由对外部DLL的调用填充,外部DLL用数据填充它。从那里我想将char []添加为ComboBox项目。
char name[256];
name[0] = "76";
comboBox1->Items->Add(name);
这会产生构建错误,因为char []不是System :: Object的类型。有关将char []转换为可以作为项添加到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;
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);
}
};
}
答案 0 :(得分: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
的字符串对象。
答案 1 :(得分:0)
尝试string strName(name);
(假设您的包含和命名空间正确...)
更新:看起来你需要使用System::String,而不是STL字符串。
答案 2 :(得分:0)
你的第二个问题听起来就像缺少字符串类的include,所以只需将#include <string>
添加到文件的顶部,你可能还需要添加using namespace std;
或使用'std :: string '取决于你的系统