简体   繁体   English

托管C ++中的字符串声明

[英]String declaration in managed c++

I am trying to declare an string variable in a c++/CLI app . 我正在尝试在c ++ / CLI应用程序中声明一个字符串变量。

My declaration looks like : 我的声明如下:

String^ strRptPath = "C:\\Reports\\NorthwindCustomers.rpt";

and i have this errors : 我有这个错误:

error C2059: syntax error : '^'

error C2238: unexpected token(s) preceding ';'

I have also tried this method : 我也尝试过这种方法:

String^ strRptPath =gcnew String("C:\\Reports\\NorthwindCustomers.rpt");

it returns the same errors . 它返回相同的错误。

The entire code is : 整个代码是:

   #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)
        {   


        }

};

} }

Am i doing somthing wrong ? 我做错什么了吗? It is my first time working in managed c++. 这是我第一次使用托管c ++。

Thanks. 谢谢。

Maybe you forgot to add using namespace System::Text; 也许您忘记了using namespace System::Text;添加using namespace System::Text; at the top of your code. 在代码的顶部。 This namespace is needed when using String^ 使用String^时需要此名称空间

It looks like you have mixed Managed C++ and C++/CLI syntax. 看来您混合使用了托管C ++和C ++ / CLI语法。
If you are using Managed C++ (not C++/CLI), the declaration of managed objects is different: 如果使用的是托管C ++(不是C ++ / CLI),则托管对象的声明是不同的:

Managed C++ syntax: 托管C ++语法:

   String __gc *RptPath = S"whatever";

C++/CLI syntax: C ++ / CLI语法:

   String^ RptPath;

Note that Managed C++ is deprecated by now, so if possible, i would suggest using C++/CLI instead (it also has a clearer syntax). 请注意,现在不推荐使用托管C ++,因此,如果可能的话,我建议改用C ++ / CLI(它也具有更清晰的语法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM