[英]String declaration in managed c++
我正在尝试在c ++ / CLI应用程序中声明一个字符串变量。
我的声明如下:
String^ strRptPath = "C:\\Reports\\NorthwindCustomers.rpt";
我有这个错误:
error C2059: syntax error : '^'
error C2238: unexpected token(s) preceding ';'
我也尝试过这种方法:
String^ strRptPath =gcnew String("C:\\Reports\\NorthwindCustomers.rpt");
它返回相同的错误。
整个代码是:
#pragma once
namespace CRViewerXI
{
using namespace System;
using namespace System::Text;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace CrystalDecisions::Windows::Forms;
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
CrystalDecisions::Windows::Forms::CrystalReportViewer *CRViewer;
System::ComponentModel::Container * components;
private : String^ strRptPath =gcnew String("C:\\Reports\\NorthwindCustomers.rpt");
void LoadReport()
{
}
void InitializeComponent(void)
{
CRViewer = new CrystalDecisions::Windows::Forms::CrystalReportViewer();
CRViewer->ActiveViewIndex = -1;
CRViewer->ShowGroupTreeButton = true;
CRViewer->ShowExportButton = true;
CRViewer->EnableToolTips = true;
CRViewer->DisplayToolbar = true;
CRViewer->Dock = System::Windows::Forms::DockStyle::Fill;
Controls->Add(CRViewer);
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(528, 394);
this->Name = S"Form1";
this->Text = S"Form1";
this->Load += new System::EventHandler(this, Form1_Load);
}
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
}
};
}
我做错什么了吗? 这是我第一次使用托管c ++。
谢谢。
也许您忘记了using namespace System::Text;
添加using namespace System::Text;
在代码的顶部。 使用String^
时需要此名称空间
看来您混合使用了托管C ++和C ++ / CLI语法。
如果使用的是托管C ++(不是C ++ / CLI),则托管对象的声明是不同的:
托管C ++语法:
String __gc *RptPath = S"whatever";
C ++ / CLI语法:
String^ RptPath;
请注意,现在不推荐使用托管C ++,因此,如果可能的话,我建议改用C ++ / CLI(它也具有更清晰的语法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.