【问题标题】:String declaration in managed c++托管 C++ 中的字符串声明
【发布时间】:2013-02-18 06:35:17
【问题描述】:

我正在尝试在 c++/CLI 应用程序中声明一个字符串变量。

我的声明如下:

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

我有这个错误:

error C2059: syntax error : '^'

错误 C2238:';' 前面的意外标记

这个方法我也试过了:

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

谢谢。

【问题讨论】:

  • 你在类内部初始化你的字符串,如果它不是静态的,那是无效的,试着把它放在一个方法中,比如说 InitializeComponent,看看它是否有效

标签: .net string declaration managed-c++


【解决方案1】:

您似乎混合使用了托管 C++ 和 C++/CLI 语法。
如果您使用的是托管 C++(不是 C++/CLI),托管对象的声明会有所不同:

托管 C++ 语法:

   String __gc *RptPath = S"whatever";

C++/CLI 语法:

   String^ RptPath;

请注意,托管 C++ 现在已被弃用,因此如果可能,我建议使用 C++/CLI(它还有更清晰的语法)。

【讨论】:

  • 这就是问题所在。谢谢。
【解决方案2】:

也许您忘记在代码顶部添加using namespace System::Text;。使用String^时需要这个命名空间

【讨论】:

  • 不,它在那里 using namespace System::Text ,它仍然给出这个错误。
  • 你能把整个代码贴出来吗?因为你使用 String^ 的方式是完全有效的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2010-12-18
  • 2014-01-03
  • 2010-12-31
  • 1970-01-01
相关资源
最近更新 更多