473,785 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in using the Graph object in different function

151 New Member
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.h pp>
#include <boost/property_map.hp p>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;

class Molecule
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
map<int,string> m;

public:
void set_molecule_pr operty(vector<s tring>& 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::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
}

void get_neighbours( )
{
//Graph g(m.size()+1);
Graph g;
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = 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,10,11,11,12 ,12,13,14,17,19 ,19,19,20,20,20 ,21,21,21};
int s12[]={13,19,14,20,1 5,21,16,18,17,1 8,16,27,28,18,3 8,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(s1 1[i]);
s2.push_back(s1 2[i]);
s3.push_back(s1 3[i]);
}


for (unsigned int i = 0; i < sizeof(s14)/sizeof(string); i++)
{
s4.push_back(s1 4[i]);
}
mol.set_molecul e_property(s4);
mol.create_chem ical_Network(s1 ,s2,s3);
mol.get_neighbo urs();
return 0;
}

Please Help me out of this pblm.I will be really thankful to you.
Apr 5 '08 #1
1 1857
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
1471
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 Graph 97. The Graph objects in the database were automatically converted to Graph 2000 objects, even though they were created in Graph 97. This creates huge problems with some of the reports that use complex graphs, because the computer runs out...
7
3224
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 is drawn by loading a jpg into a bitmap and then setting picturebox.image = mybitmap. Ive created my own class, inheriting from the picturebox and overriding the OnPaint event so I can do e.Graphics.DrawImage (this.image....). This renders...
1
2811
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 axises? For example instead of the percent safety audits closed on the x axis, i would put the safety audit date? I believe its a little different than excel I do not have alot of VBA experience.
5
3041
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 simple code to create a graph in an existing php page. I added the code for the graph and added the elements of it plus some static data. However, when I browse the page, I get garbled data instead of the graph. Has anyone ever run into this...
2
2086
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: Suppose that there is a table named (TPerson) in which field names are "Name"<text>", "Age"<number-byte>. I would like to get a simple graph, X-axis stands for "Name", Y-axis stands for "Age". I follow procedure by using wizard but result is...
5
3647
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 have been unable to find how to specifically refer to properties within a graph object. Below is a snippet of code from my module in which the graph ('chart_DataSet') is in a subform ('frm_sub_Chart') that is contained within a control...
4
2557
by: Man4ish | last post by:
namespace ve/////////////////ve.h { struct VertexProperties { std::size_t index; boost::default_color_type color; }; } /////////////////////////////////////////////////////////////////////////////////////////////////// namespace ed///////////////////////ed.h
2
2317
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 <boost/graph/adjacency_list.hpp> #include <boost/tuple/tuple.hpp> #include <set> using namespace std;
6
2268
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. There is also an MFC dll that is called from this asp application to make a syntax check on quite many commands. You don't have to know what a command is. The problem that we get is the following when the asp pages calls the MFC
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.