I am trying to compile some code (Example.cpp from here: http://www.rpi.edu/~doriad/Daniweb/maxflow/). I am getting

Example.cpp:(.text+0x38): undefined reference to `Graph<int, int, int>::Graph(int, int, void (*)(char*))'

This is clearly a template instantiation problem. I see that there is a file called instances.inc that defines the <int,int,int> class, do I need to somehow tell gcc/g++ to use this file? Or how else could I get this to work?

Thanks,

Dave

Here is a simplified version that produces the error:

graph.h

#include <string.h>


template <typename captype, typename tcaptype, typename flowtype> class Graph

{

public:


	Graph(int node_num_max, int edge_num_max, void (*err_function)(char *) = NULL);

};

graph.cpp

#include "graph.h"

#include "instances.inc"



template <typename captype, typename tcaptype, typename flowtype> 

	Graph<captype, tcaptype, flowtype>::Graph(int node_num_max, int edge_num_max, void (*err_function)(char *))

{



}

instances.inc

#include "graph.h"



template class Graph<int,int,int>;

template class Graph<short,int,int>;

template class Graph<float,float,float>;

template class Graph<double,double,double>;

Example.cpp

#include <stdio.h>

#include "graph.h"



int main()

{

	typedef Graph<int,int,int> GraphType;

	GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1); 



	return 0;

}

Any ideas?

Thanks,
Dave

Does any one have any clues for me?

Dave

Does any one have any clues for me?

Dave

are u compiling it as

c++ Example.cpp graph.cpp

If not then do it like that.

I am doing that:

[doriad@davedesktop test]$ g++ Example.cpp graph.cpp
/tmp/ccHbyqCi.o: In function `main':
Example.cpp:(.text+0x38): undefined reference to `Graph<int, int, int>::Graph(int, int, void (*)(char*))'
collect2: ld returned 1 exit status

Can anyone confirm this behavior?

GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1);

u are calling constructor

Graph<captype, tcaptype, flowtype>::Graph(int, int);

but u only have a constructor with prtotype

Graph<captype, tcaptype, flowtype>::Graph(int , int, void (*err_function)(char *));

Hence prototype mismatch. Correct it.

I dont think so ...
The constructor is actually:

Graph(int node_num_max, int edge_num_max, void (*err_function)(char *) = NULL);

which is an optional parameter, no? Hence it should be fine to call it with only 2 ints?

I dont think so ...
The constructor is actually:

Graph(int node_num_max, int edge_num_max, void (*err_function)(char *) = NULL);

which is an optional parameter, no? Hence it should be fine to call it with only 2 ints?

hey sorry for that. I skipped it.

The file graph.h is getting included twice in graph.cpp beacuse u are including it from instances.inc also.
Remove the

#include"graph.h"

statement from the file instances.inc.

I tried it in my compiler and i didn't get any error.

NB: whenever u are making some header file its good to define a macro such that

example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

/*prototypes*/


#endif

so that the same doesn't get included more than once.

I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/

with

g++ Example.cpp graph.cpp

I am not sure of why u are getting the error. I am compiling it as follows and m not getting any error in my machine.

myself@king  [70]g++ Example.cpp graph.cpp
myself@king  [71]

Interesting - can anyone else confirm?
I am using:
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)

Interesting - can anyone else confirm?
I am using:
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)

i used
g++ (GCC) 3.3.2 20040119 (Red Hat Linux 3.3.2-8) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.