gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:18:14 26 4
gpt4 key购买 nike

我不太清楚为什么创建的 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;
}
}
}
};

最佳答案

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

在 Form1() 之外,edge_array 是未定义的。

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

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

关于c++ - 在Windows窗体c++/cli程序中为已定义的标识符获取未定义的标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6924753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com