2013-03-04 69 views
0

我想在C++/CLI应用程序中声明一个字符串变量。托管C++中的字符串声明

我的声明如下:

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++。

谢谢。

+0

你正在初始化你的字符串在类本身内部,如果它不是静态的,那么它是无效的,试着把它放在一个方法里面,让我们说InitializeComponent,看看它是否有效 – TravellingGeek 2013-03-04 08:44:53

回答

0

看起来您已将托管C++和C++/CLI语法混合使用。
如果您使用托管C++(未C++/CLI),管理对象的声明是不同的:

托管C++语法:

String __gc *RptPath = S"whatever"; 

C++/CLI语法:

String^ RptPath; 

请注意,Managed C++现在已被弃用,所以如果可能的话,我会建议使用C++/CLI来代替(它也有更清晰的语法)。

+0

这是问题。谢谢。 – 2013-03-04 08:50:50

0

也许你忘了在代码顶部添加using namespace System::Text;了。使用此名称空间时,需要使用String^

+0

不,它在那里using namespace System::Text,它仍然给出这个错误。 – 2013-03-04 08:09:21

+0

你能发布整个代码吗?因为你使用String ^的方式是完全有效的 – TravellingGeek 2013-03-04 08:12:54