Hello,

I'm getting an error when I try to compile the code and I do not know how to solve the problem. Please help! The assignment asks me to create a BOOLEAN function that can check if the graph is connected or not. So I decided to use DFS approach to this problem. But now my code does not want to compile please help. ERROR LNK 2019.

My code and other files necessary to run will be included in an attachment.

//*******************************************************************************************
// Program includes
// *******************************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>
#include <deque>
#include <vector>
#include "rstream.h"
using namespace std;


// *******************************************************************************************
// Function prototypes
// *******************************************************************************************
// Builds the representation of the network as random graph
void buildRG( int n,							// n is the number of the vertices
			 double pr,							// Probability (a number 0 < pr < 1) to have an edge between two vertex
			 vector< deque< int > > &adjl,		// Adjacency list
			 int &l );							// Number of edges

// Prints the graph representation: Adj. list
// Used for testing
void printAList( int n, const vector< deque< int > > &l );

//Function for searching connected vertices
bool DFS_Con(int n,								// n is the number of the vertices
			 vector< deque< int > > &adjl);		// Adjacency list

//Function for exploring edges
void DFS_VISIT (const vector< deque<int> > &adjl_1,	// Adjacency list
				int u,							// Current vertex counter
				vector <bool> &col,				// Store colors of Vetices
				int x);							// n is the number of the vertices


// *******************************************************************************************
// Definitions and calls withing your main() function
// *******************************************************************************************

int main ()
{

int nn = 0;
double edgeProb = 0;

// Total number of vertices in the graph. Asked as input from the user
cout<<"Enter total number of Vertices: ";
cin>>nn;
cout<<endl<<endl;


// Probability of an edge between two vertices. It can be asked as input from the user
cout<<"Enter probability between two Vertices: ";
cin>> edgeProb;
cout<<endl<<endl;


// Adjacency list
vector< deque< int > > G( nn );

// Number of edges
int ll = 0;

// Fucntion call for generating random graphs
buildRG( nn, edgeProb, G, ll );

// Function call for displaying Adjacency List
printAList( nn, G );

// Function call for searching connected vertices
if (DFS_Con(nn, G )== true)
cout<<"connected";
else
cout<<"not connected";

cin>>nn;

return 0;

}


// *******************************************************************************************
// Function definitions
// *******************************************************************************************
// Generates representation of random graphs 
void buildRG( int n,
			 double pr,
			 vector< deque< int > > &adjl, 
			 int &l ) {
	
	static Rstream prob( 284723743 );
	
	int nl = 0; // The number of edges
	
	// Build up the adj. list, the adj matrix and count the number of edges
	for( int a = 0; a < n; a++ )
		for( int b =  a + 1; b < n; b++ )
			if ( prob.uniform01() <= pr )
			{
				adjl[ a ].push_front( b );
				adjl[ b ].push_front( a );
				nl++;
			}

	// Total number of edges
	l = nl;
	
} // End buildRG


//---------------------------------------------------------------------
// Printing out graph representations: Adj. list
void printAList( int n, const vector< deque< int > > &l ) {
	
	for( int i = 0; i < n; i++ ) {
		cout << "Node " << i << " list:";
		for( unsigned int j = 0; j < l[ i ].size(); j++ )
			cout << " -> " << l[ i ][ j ];
		cout << endl; 
	}
	
} // End printAList


//---------------------------------------------------------------------
//Finding connected vertices
bool DFS_Con (int n, const vector< deque <int> > &adjl){

	vector <bool> color (n+1,false);	//false = white, true = black

	for ( int b=0; b<n; b++)
		if (color[b] == false)
			DFS_VISIT (adjl, b, color, n);

	
return  color[n];
			
} // End DFS_Con

//--------------------------------------------------------------------
//Visiting vertices
void DFS_VISIT (const vector< deque<int> > &adjl_1, int u, vector <bool> &col, int x){

	col[u] = true;

	for (unsigned int a = 0; a < adjl_1[u].size(); a++)
		if (col[adjl_1[u][a]] == false){
			col[x] = true;
			DFS_VISIT (adjl_1, adjl_1[u][a] , col, x);
		}

} // End DFS_VISIT
Attachments
// *******************************************************************************************
// Program includes
// *******************************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>
#include <deque>
#include <vector>
#include "rstream.h"
using namespace std;


// *******************************************************************************************
// Function prototypes
// *******************************************************************************************
// Builds the representation of the network as random graph
void buildRG( int n,							// n is the number of the vertices
			 double pr,							// Probability (a number 0 < pr < 1) to have an edge between two vertex
			 vector< deque< int > > &adjl,		// Adjacency list
			 int &l );							// Number of edges

// Prints the graph representation: Adj. list
// Used for testing
void printAList( int n, const vector< deque< int > > &l );

//Function for searching connected vertices
bool DFS_Con(int n,								// n is the number of the vertices
			 vector< deque< int > > &adjl);		// Adjacency list

//Function for exploring edges
void DFS_VISIT (const vector< deque<int> > &adjl_1,	// Adjacency list
				int u,							// Current vertex counter
				vector <bool> &col,				// Store colors of Vetices
				int x);							// n is the number of the vertices


// *******************************************************************************************
// Definitions and calls withing your main() function
// *******************************************************************************************

int main ()
{

int nn = 0;
double edgeProb = 0;

// Total number of vertices in the graph. Asked as input from the user
cout<<"Enter total number of Vertices: ";
cin>>nn;
cout<<endl<<endl;


// Probability of an edge between two vertices. It can be asked as input from the user
cout<<"Enter probability between two Vertices: ";
cin>> edgeProb;
cout<<endl<<endl;


// Adjacency list
vector< deque< int > > G( nn );

// Number of edges
int ll = 0;

// Fucntion call for generating random graphs
buildRG( nn, edgeProb, G, ll );

// Function call for displaying Adjacency List
printAList( nn, G );

// Function call for searching connected vertices
DFS_Con(nn, G );

cin>>nn;

return 0;

}


// *******************************************************************************************
// Function definitions
// *******************************************************************************************
// Generates representation of random graphs 
void buildRG( int n,
			 double pr,
			 vector< deque< int > > &adjl, 
			 int &l ) {
	
	static Rstream prob( 284723743 );
	
	int nl = 0; // The number of edges
	
	// Build up the adj. list, the adj matrix and count the number of edges
	for( int a = 0; a < n; a++ )
		for( int b =  a + 1; b < n; b++ )
			if ( prob.uniform01() <= pr )
			{
				adjl[ a ].push_front( b );
				adjl[ b ].push_front( a );
				nl++;
			}

	// Total number of edges
	l = nl;
	
} // End buildRG


//---------------------------------------------------------------------
// Printing out graph representations: Adj. list
void printAList( int n, const vector< deque< int > > &l ) {
	
	for( int i = 0; i < n; i++ ) {
		cout << "Node " << i << " list:";
		for( unsigned int j = 0; j < l[ i ].size(); j++ )
			cout << " -> " << l[ i ][ j ];
		cout << endl; 
	}
	
} // End printAList


//---------------------------------------------------------------------
//Finding connected vertices
bool DFS_Con (int n, const vector< deque <int> > &adjl){

	vector <bool> color (n+1,false);	//false = white, true = black

	for ( int b=0; b<n; b++)
		if (color[b] == false)
			DFS_VISIT (adjl, b, color, n);

	
return  color[n];
			
} // End DFS_Con

//--------------------------------------------------------------------
//Visiting vertices
void DFS_VISIT (const vector< deque<int> > &adjl_1, int u, vector <bool> &col, int x){

	col[u] = true;

	for (unsigned int a = 0; a < adjl_1[u].size(); a++)
		if (col[adjl_1[u][a]] == false){
			col[x] = true;
			DFS_VISIT (adjl_1, adjl_1[u][a] , col, x);
		}

} // End DFS_VISIT
#include <math.h>

#include "rstream.h"

const double PII = 3.141592654;

const unsigned long int FACTOR = 663608941;

const double max_value = 4294967295.0; // (2**(32))-1

// This is used in the lngamma function
const float cof[6] = {76.18009173, -86.50532033, 24.01409822,
                      -1.231739516, 0.120858003e-2, -0.536382e-5};


/***********************************************************************
** function:    Rstream, constructor;
** parameters:  unsigned long int, a seed s;
** description: Create a stream with a seed s.
***********************************************************************/
Rstream::Rstream( unsigned long int s ) {

  xi = ( s % 2 ) ? s :( s + 1 ); // Make sure the seed is an odd number

} // End constructor


/***********************************************************************
** function:    uniform01;
** parameters:  none;
** description: Generate a random number between 0 and 1 based on the
**              relation X(i+1)  =  (X(i) * 663608941) mod (2**32).
***********************************************************************/
double Rstream::uniform01() {

  xi *= FACTOR;
  // This includes the '% 2**32', since the g++ arithmetic is % 2**32

  return ( (double)xi / max_value );

} // End uniform01


/***********************************************************************
** function:    uniform;
** parameters:  double, lower and upper;
** description: Generate a random number between lower and upper.
***********************************************************************/
double Rstream::uniform( double lower, double upper ) {

  return ( uniform01() * (upper - lower) + lower );

} // End uniform


/***********************************************************************
** function:    lngamma;
** parameters:  float, xx;
** description: Return log of gamma function of xx for xx > 0.
***********************************************************************/
float Rstream::lngamma( float  xx ) const {

  float  x, tmp, ser;
  int j;

  x = xx - 1.0;
  tmp = x + 5.5;
  tmp -= ( x + 0.5 ) * log( tmp );
  ser = 1.0;
  for ( j = 0; j < 6; j++ ) {
         x += 1.0;
         ser += cof[ j ] / x;
  }

  return ( -tmp + log( 2.50662827465 * ser ) );

} // End lngamma


/***********************************************************************
** function:    poisson;
** parameters:  float, mean;
** description: Return a random variable drawn from the poisson
**              distribution with average value mean. The value returned 
**              represents the number of unit rate poisson events 
**              occurring.
***********************************************************************/
double Rstream::poisson( float mean ) {

  static float sq, alxm, g, oldm = -1.0;
  float  em, t, y;

  /*
  ** Can we use direct method
  */
  if ( mean < 12.0 ) {
    if ( mean != oldm ) {
      oldm = mean;
      g    = exp( -mean );
    }
    em = -1.0;
    t  = 1.0;
    do {
      em += 1.0;
      t *= uniform01();
    } while ( t > g );
  }
  /*
  ** If not, use rejection method
  */
  else {
    if ( mean != oldm ) {
      oldm = mean;
      sq   = sqrt( 2.0 * mean );
      alxm = log( mean );
      g    = mean * alxm - lngamma( mean + 1.0 );
    }
    do {
      do {
        /*
        ** y is a deviate from a Lorentzian comparison function
        */
        y    = tan ( PII * uniform01() );
        em   = sq * y + mean;
      } while ( em < 0.0 );
      /*
      ** poisson is integer valued
      */
      em = floor( em );
      /*
      ** t is the ration of the desired distribution to the comparison
      ** function - accept or reject by comparing it to the the uniform
      ** deviate. Factor 0.9 is chosen so that it never exceeds 1.0
      */
      t = 0.9 * ( 1.0 + y * y ) * exp( em * alxm - 
				       lngamma( em + 1.0 ) - g );
    } while ( uniform01() > t );
  }

  return em;

} // End poisson


/***********************************************************************
** function:    uniform_discrete
** parameters:  int, lower, upper
** description: Generate a discrete random number between lower and 
**              upper (lower and upper included).
***********************************************************************/
unsigned long int Rstream::uniform_discrete( unsigned long int lower, 
					     unsigned long int upper ) {

  return ( ( unsigned long int )floor( uniform01()*( upper - lower + 1 ) 
				       + lower ) );

} // End uniform_discrete
//----------------------------------------------------------------------

#ifndef RSTREAM_H
#define RSTREAM_H

class Rstream{
 public:

   Rstream( unsigned long int );
   unsigned long int get_xi() const { return xi; };
   double uniform01();
   double uniform( double, double ); 
   double poisson( float );
   unsigned long int uniform_discrete( unsigned long int, 
				       unsigned long int );
 private:

   unsigned long int xi; 
   float lngamma ( float ) const;

};

#endif

Error LNK2019 relates to the failure of the compiler to locate an external symbol referenced in a function (linker error). Is your rstream header file in the same folder as your CPP file?
Whats the full error message? Mind to post it here?

This article has been dead for over six months. Start a new discussion instead.