473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem in using the Graph object in different function

151 100+
I am creating a graph using boost library, I am making the network using create_Network function()
i am adding the vertices to this graph by creating the object g of class Graph.
I am trying to use this object g in the another function get_neighbours()
but it is not giving any result.Should I make it global or should use constructor so that values stored in the graph(vertices) object(g), can be used in any function.

#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map.hpp>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;

class Molecule
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
map<int,string> m;

public:
void set_molecule_property(vector<string>& s4)
{
for (unsigned int i = 1; i <= s4.size(); i++)
{
m[i]=s4[i-1];
}
}



void create_chemical_Network(vector<int> s1,vector<int> s2,vector<int> s3)
{
const int V = m.size()+1;
Graph g(V);
for (int i = 0; i <V ; i++)
{
if(s3[i]==1){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("triple_bond"), g);
}
}
}

void get_neighbours()
{
//Graph g(m.size()+1);
Graph g;
property_map<Graph, std::size_t ve::VertexProperties::*>::type
id = get(&ve::VertexProperties::index, g);
property_map<Graph, std::string ed::EdgeProperties::*>::type
name = get(&ed::EdgeProperties::name, g);
boost::graph_traits<Graph>::vertex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi,viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
graph_traits<Graph>::vertex_iterator i, end;
graph_traits<Graph>::out_edge_iterator ei, edge_end;
for (boost::tie(i,end) = vertices(g); i != end; ++i)
{
if(id[*i]!=0)
{
cout << id[*i]<<"("<<m[id[*i]]<<")" << " ";
for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << " --" << name[*ei] << "--> " << m[id[target(*ei, g)]] << " ";
cout << endl;
}
}
print_edges(g, id);
cout<<endl;
print_vertices(g,id);
}
};

int main(int,char* [])
{
using namespace boost;
Molecule mol;
int s11[]={1,1,2,2,3,3,4,4,5,5,6,6,6,7,7,7,8,8,8,8,9,9,10,1 0,11,11,12,12,13,14,17,19,19,19,20,20,20,21,21,21} ;
int s12[]={13,19,14,20,15,21,16,18,17,18,16,27,28,18,38,39, 9,10,22,23,11,12,16,17,14,24,13,25,15,15,26,29,30, 31,32,33,34,35,36,37};
int s13[]={1,1,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,2, 1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,1};
string s14[]={"O","O","O","N","N","N","N","C","C","C","C","C", "C","C","C","C","C","C","C","C","C","H","H","H","H ","H","H","H","H","H","H","H","H","H","H","H","H", "H","H"};
vector <string> s4;
vector <int> s1,s2,s3;

for (unsigned int i = 0; i < sizeof(s11)/sizeof(string); i++)
{
s1.push_back(s11[i]);
s2.push_back(s12[i]);
s3.push_back(s13[i]);
}


for (unsigned int i = 0; i < sizeof(s14)/sizeof(string); i++)
{
s4.push_back(s14[i]);
}
mol.set_molecule_property(s4);
mol.create_chemical_Network(s1,s2,s3);
mol.get_neighbours();
return 0;
}

Please Help me out of this pblm.I will be really thankful to you.
Apr 5 '08 #1
1 1832
weaknessforcats
9,208 Expert Mod 8TB
Your Graph objects are local to the funciton. Local variables are destroyed when the function ends.

The Graph object g in get_neighbours() is not the one created in create_chemical_Network().

You need to create your Graph object on the heap and then pass a pointer to it to your various functions.

Also, get_neighbours() has no arguments and returns a void. That means your entire application is inside this thing, in which case having the funciton at all is pointless. Or you are using global variables which are a disaster waiting to happen.

If you are using global variables, the you should read the Case Against Global Variables in the C/C++ HowTos forum.
Apr 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Christine Lawrie | last post by:
I have an Access 97 database that has graphs installed on a PC with both Office 97 and Office 2000. I know other people are aware of this - the database automatically uses Graph 2000 instead of...
7
by: news | last post by:
This may be a stupid question, but if I don't ask I'll never know ;) Ok, here it goes.... I am writing an application that renders an image in one picturebox and a graph in another. The image...
1
by: Heather | last post by:
I have created a unbound object frame and selected MS Graph and use my table to create my graph. Note I did not use the wizard. I open my MS Graph to change the stuff. How can I change the...
5
by: Shawn Hamzee | last post by:
Hello All, I am having a problem with Image_Graph on php 5.1.4. I installed the package and all of its dependencies through pear installer without any hitches. Then I started to add some very...
2
by: savas_karaduman | last post by:
I supposed that creating graph with access would be as easy as how I could with Excel. But it seems to me that there is some tricky point here. What i am trying to do and what i got is as follow: ...
5
by: CanOfWorms | last post by:
Access 2003 (previous 2000) Windows XP Pro I am fairly new at VBA within Access, learning as I go thorugh forums and discussion groups. I generally find my answers through previous posts, but I...
4
by: Man4ish | last post by:
namespace ve/////////////////ve.h { struct VertexProperties { std::size_t index; boost::default_color_type color; }; }...
2
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include...
6
by: Tony Johansson | last post by:
Hello! We have a C#.ASP.NET application that runs on a IIS 6. The application contains actually several asp pages but there is no GUI. The application receive an xml file that is processed....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.