在Windows窗体c ++ / cli程序中为已定义的标识符获取未定义的标识符错误

Posted

技术标签:

【中文标题】在Windows窗体c ++ / cli程序中为已定义的标识符获取未定义的标识符错误【英文标题】:Getting undefined identifier error for already defined identifier in windows forms c++/cli program 【发布时间】:2011-08-03 10:04:12 【问题描述】:

我不太确定为什么创建的 edge_array 在按钮单击处理程序中使用时显示为未定义的标识符而且我还在 cli 托管的东西中使用了 boost 非托管代码,任何帮助都将不胜感激

namespace Prototype 
    //using namespace boost;
    using namespace boost::graph;
    using namespace boost::numeric::ublas;
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Runtime::InteropServices;
    using namespace msclr::interop;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    //graph properties types
    typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
    typedef boost::property<boost::vertex_name_t, std::string, boost::property<boost::vertex_potential_t, int, boost::property<boost::vertex_update_t, double> > > StationProperties;

    //graph type
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, StationProperties, EdgeWeightProperty> Graph;

    //instantiation
    Graph g;

    // Property accessors
    boost::property_map<Graph, boost::vertex_name_t>::type station_name = get(boost::vertex_name, g);
    boost::property_map<Graph, boost::vertex_potential_t>::type station_capacity = get(boost::vertex_potential, g);
    boost::property_map<Graph, boost::vertex_update_t>::type station_update = get(boost::vertex_update, g);
    boost::property_map<Graph, boost::edge_weight_t>::type edge_distance = get(boost::edge_weight, g);

    //vertex descriptor
    typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;

    //transition matrix definition and station vector definition
    matrix<double> trans(6,6);
    vector<double> stat(6);

    bool inserted;
    int counter = 0;

    typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;

    edge_descriptor e;

    public ref class Form1 : public System::Windows::Forms::Form
    
    public:
        Form1(void)
        
            InitializeComponent();

            //station vector definition
            stat(0) = 1000;
            stat(1) = 1000;
            stat(2) = 1000;
            stat(3) = 1000;
            stat(4) = 1000;
            stat(5) = 1000;

            //transition matrix
            trans(0,0) = 0; trans(0,1) = 1; trans(0,2) = 0; trans(0,3) = 0; trans(0,4) = 0; trans(0,5) = 0;
            trans(1,0) = 0.25; trans(1,1) = 0; trans(1,2) = 0.75; trans(1,3) = 0; trans(1,4) = 0; trans(1,5) = 0;
            trans(2,0) = 0; trans(2,1) = 0.33; trans(2,2) = 0; trans(2,3) = 0.33; trans(2,4) = 0.33; trans(2,5) = 0;
            trans(3,0) = 0; trans(3,1) = 0; trans(3,2) = 0.75; trans(3,3) = 0; trans(3,4) = 0; trans(3,5) = 0.25;
            trans(4,0) = 0; trans(4,1) = 0; trans(4,2) = 0.75; trans(4,3) = 0; trans(4,4) = 0; trans(4,5) = 0.25;
            trans(5,0) = 0; trans(5,1) = 0; trans(5,2) = 0; trans(5,3) = 0.5; trans(5,4) = 0.5; trans(5,5) = 0;

            //station graph declerations

            Vertex u0;
            u0 = add_vertex(g);
            station_name[u0] = "Kennington";
            station_capacity[u0] = 2000;
            station_update[u0] = stat(0);

            Vertex u1;
            u1 = add_vertex(g);
            station_name[u1] = "Elephant & Castle";
            station_capacity[u1] = 2000;
            station_update[u1] = stat(1);

            Vertex u2;
            u2 = add_vertex(g);
            station_name[u2] = "London Bridge";
            station_capacity[u2] = 3000;
            station_update[u2] = stat(2);

            Vertex u3;
            u3 = add_vertex(g);
            station_name[u3] = "Bank";
            station_capacity[u3] = 3000;
            station_update[u3] = stat(3);

            Vertex u4;
            u4 = add_vertex(g);
            station_name[u4] = "Borough";
            station_capacity[u4] = 2000;
            station_update[u4] = stat(4);

            Vertex u5;
            u5 = add_vertex(g);
            station_name[u5] = "Oval";
            station_capacity[u5] = 3000;
            station_update[u5] = stat(5);

            typedef std::pair<int,int> Edge;
            Edge edge_array[] = 
                Edge(u0, u0), Edge(u0, u1),Edge(u0, u2),Edge(u0, u3),Edge(u0, u4),Edge(u0, u5),
                Edge(u1, u0), Edge(u1, u1),Edge(u1, u2),Edge(u1, u3),Edge(u1, u4),Edge(u1, u5),
                Edge(u2, u0), Edge(u2, u1),Edge(u2, u2),Edge(u2, u3),Edge(u2, u4),Edge(u2, u5),
                Edge(u3, u0), Edge(u3, u1),Edge(u3, u2),Edge(u3, u3),Edge(u3, u4),Edge(u3, u5),
                Edge(u4, u0), Edge(u4, u1),Edge(u4, u2),Edge(u4, u3),Edge(u4, u4),Edge(u4, u5),
                Edge(u5, u0), Edge(u5, u1),Edge(u5, u2),Edge(u5, u3),Edge(u5, u4),Edge(u5, u5)
            ;
        //all other declerations

    private:
        void Build_Click(System::Object^  sender, System::EventArgs^  e) 
            //station_name[u0] = marshal_as<std::string> (this->NameBox0->Text);
            station_name[0] = marshal_as<std::string> (this->NameBox0->Text);
            station_name[1] = marshal_as<std::string> (this->NameBox1->Text);
            station_name[2] = marshal_as<std::string> (this->NameBox2->Text);
            station_name[3] = marshal_as<std::string> (this->NameBox3->Text);
            station_name[4] = marshal_as<std::string> (this->NameBox4->Text);
            station_name[5] = marshal_as<std::string> (this->NameBox5->Text);

            station_capacity[0] = System::Convert::ToInt32(this->Capbox0->Text);
            station_capacity[1] = System::Convert::ToInt32(this->Capbox1->Text);
            station_capacity[2] = System::Convert::ToInt32(this->Capbox1->Text);
            station_capacity[3] = System::Convert::ToInt32(this->Capbox1->Text);
            station_capacity[4] = System::Convert::ToInt32(this->Capbox1->Text);
            station_capacity[5] = System::Convert::ToInt32(this->Capbox5->Text);

            for (unsigned i = 0; i < trans.size1 (); ++ i)
                for (unsigned j = 0; j < trans.size2 (); ++ j)
                    if (trans(i,j) > 0.1)
                    
                        boost::tie(e, inserted) = boost::add_edge(edge_array[counter].first,edge_array[counter].second,g);
                        edge_distance[e] = trans(i,j);
                    
                    ++counter;
                
            
        
    ;

【问题讨论】:

【参考方案1】:

您在 Form1 构造函数中声明和初始化 edge_array。由于您在 Form1() 的范围内声明变量,因此它仅存在于 Form1() 的范围内。

Form1() 之外的 edge_array 未定义。

要使 edge_array 对 Button_Click 可见,您需要在类外声明它,例如声明 vector&lt;double&gt; stat 或像其他类成员变量一样在类内声明它 - 在 //all other declerations 区域的某处。

您仍然可以在构造函数中初始化数组。

【讨论】:

以上是关于在Windows窗体c ++ / cli程序中为已定义的标识符获取未定义的标识符错误的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中为 Windows 窗体创建向导

在 C++/CLI 中并行执行程序

C# Windows 窗体应用程序“参数无效。”

如何在 Windows 窗体应用程序中显示 MFC 控件?

如何在 Windows 窗体 C# 中为文本框设置 Scintilla

Visual Studio 2010 在编译 C++/CLI 项目时出现奇怪的错误