944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1031
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 8th, 2009
0

instances.inc?

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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 9th, 2009
0
Re: instances.inc?
Here is a simplified version that produces the error:

graph.h
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 12th, 2009
0
Re: instances.inc?
Does any one have any clues for me?

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 12th, 2009
0
Re: instances.inc?
Click to Expand / Collapse  Quote originally posted by daviddoria ...
Does any one have any clues for me?

Dave
are u compiling it as
C++ Syntax (Toggle Plain Text)
  1. c++ Example.cpp graph.cpp
If not then do it like that.
Last edited by dkalita; Oct 12th, 2009 at 8:33 am.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 12th, 2009
0
Re: instances.inc?
I am doing that:
C++ Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 12th, 2009
0
Re: instances.inc?
Click to Expand / Collapse  Quote originally posted by daviddoria ...
C++ Syntax (Toggle Plain Text)
  1. GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1);
u are calling constructor
C++ Syntax (Toggle Plain Text)
  1. Graph<captype, tcaptype, flowtype>::Graph(int, int);
but u only have a constructor with prtotype
C++ Syntax (Toggle Plain Text)
  1. Graph<captype, tcaptype, flowtype>::Graph(int , int, void (*err_function)(char *));
Hence prototype mismatch. Correct it.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 12th, 2009
0
Re: instances.inc?
I dont think so ...
The constructor is actually:
C++ Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 12th, 2009
0
Re: instances.inc?
Click to Expand / Collapse  Quote originally posted by daviddoria ...
I dont think so ...
The constructor is actually:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 12th, 2009
0
Re: instances.inc?
I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/

with
C++ Syntax (Toggle Plain Text)
  1. g++ Example.cpp graph.cpp
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 12th, 2009
0
Re: instances.inc?
Click to Expand / Collapse  Quote originally posted by daviddoria ...
I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/

with
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. myself@king [70]g++ Example.cpp graph.cpp
  2. myself@king [71]
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ascending order
Next Thread in C++ Forum Timeline: Multimap problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC