我不太确定为什么创建的edge_array在按钮点击处理程序中使用时显示为未定义的标识符我是cli / c ++的新手傻错误将是一个可能的原因进一步说明我正在使用VS2010我也在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;
}
}
}
};
答案 0 :(得分:1)
您在Form1构造函数中声明并初始化edge_array。由于您在Form1()范围内声明变量,因此它仅存在于Form1()的范围内。
在Form1()之外,edge_array未定义。
要使edge_array对Button_Click可见,您需要在类之外声明它vector<double> stat
,或者在类中声明它,就像其他类成员变量一样 - 在//all other declerations
区域的某个地方。
您仍然可以在构造函数中初始化数组。