instances.inc?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

instances.inc?

 
0
  #1
Oct 8th, 2009
I am trying to compile some code (Example.cpp from here: http://www.rpi.edu/~doriad/Daniweb/maxflow/). I am getting

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

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #2
Oct 9th, 2009
Here is a simplified version that produces the error:

graph.h
  1. #include <string.h>
  2.  
  3.  
  4. template <typename captype, typename tcaptype, typename flowtype> class Graph
  5.  
  6. {
  7.  
  8. public:
  9.  
  10.  
  11. Graph(int node_num_max, int edge_num_max, void (*err_function)(char *) = NULL);
  12.  
  13. };

graph.cpp
  1. #include "graph.h"
  2.  
  3. #include "instances.inc"
  4.  
  5.  
  6.  
  7. template <typename captype, typename tcaptype, typename flowtype>
  8.  
  9. Graph<captype, tcaptype, flowtype>::Graph(int node_num_max, int edge_num_max, void (*err_function)(char *))
  10.  
  11. {
  12.  
  13.  
  14.  
  15. }

instances.inc
  1. #include "graph.h"
  2.  
  3.  
  4.  
  5. template class Graph<int,int,int>;
  6.  
  7. template class Graph<short,int,int>;
  8.  
  9. template class Graph<float,float,float>;
  10.  
  11. template class Graph<double,double,double>;

Example.cpp
  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "graph.h"
  6.  
  7.  
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. typedef Graph<int,int,int> GraphType;
  14.  
  15. GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1);
  16.  
  17.  
  18.  
  19. return 0;
  20.  
  21. }

Any ideas?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #3
Oct 12th, 2009
Does any one have any clues for me?

Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #4
Oct 12th, 2009
Originally Posted by daviddoria View Post
Does any one have any clues for me?

Dave
are u compiling it as
  1. c++ Example.cpp graph.cpp
If not then do it like that.
Last edited by dkalita; Oct 12th, 2009 at 8:33 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #5
Oct 12th, 2009
I am doing that:
  1. [doriad@davedesktop test]$ g++ Example.cpp graph.cpp
  2. /tmp/ccHbyqCi.o: In function `main':
  3. Example.cpp:(.text+0x38): undefined reference to `Graph<int, int, int>::Graph(int, int, void (*)(char*))'
  4. collect2: ld returned 1 exit status

Can anyone confirm this behavior?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #6
Oct 12th, 2009
Originally Posted by daviddoria View Post
  1. GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1);
u are calling constructor
  1. Graph<captype, tcaptype, flowtype>::Graph(int, int);
but u only have a constructor with prtotype
  1. Graph<captype, tcaptype, flowtype>::Graph(int , int, void (*err_function)(char *));
Hence prototype mismatch. Correct it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #7
Oct 12th, 2009
I dont think so ...
The constructor is actually:
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #8
Oct 12th, 2009
Originally Posted by daviddoria View Post
I dont think so ...
The constructor is actually:
  1. 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
  1. #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
  1. #ifndef EXAMPLE_H
  2. #define EXAMPLE_H
  3.  
  4. /*prototypes*/
  5.  
  6.  
  7. #endif
so that the same doesn't get included more than once.
Last edited by dkalita; Oct 12th, 2009 at 9:20 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #9
Oct 12th, 2009
I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/

with
  1. g++ Example.cpp graph.cpp
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #10
Oct 12th, 2009
Originally Posted by daviddoria View Post
I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/

with
  1. 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.
  1. myself@king [70]g++ Example.cpp graph.cpp
  2. myself@king [71]
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 410 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC