Question

I am trying to declare an string variable in a c++/CLI app .

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

Thanks.

Was it helpful?

Solution

It looks like you have mixed Managed C++ and C++/CLI syntax.
If you are using Managed C++ (not C++/CLI), the declaration of managed objects is different:

Managed C++ syntax:

   String __gc *RptPath = S"whatever";

C++/CLI syntax:

   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).

OTHER TIPS

Maybe you forgot to add using namespace System::Text; at the top of your code. This namespace is needed when using String^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top