Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Template problems in gcc 3.4.2 Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Betelgeuse

March 01, 2005, 06:00 PM

I had the following code working in an earlier version of gcc but it suddenly stopped working when I upgraded DevC++.

  1.  
  2. #include <cstdlib>
  3.  
  4.  
  5.  
  6. class visitor;
  7.  
  8. template <class T, class B>
  9. class visitable: public B
  10. {
  11. public:
  12.         visitable(){};
  13.         virtual void accept(visitor &V) { V.visit((T*)this); }
  14. };
  15.  
  16. class ast_node
  17. {
  18. public:
  19.         virtual void accept(visitor& V) = 0;
  20. };
  21.  
  22. class visitor
  23. {
  24. public:
  25.         inline void dispatch(ast_node *Base) { Base->accept(*this); }
  26.  
  27.         virtual bool visit(ast_node *AstNode); 
  28. };
  29.  
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.     return EXIT_SUCCESS;
  34. }
  35.  


I'm getting the following errors:

  1.  
  2. main.cpp: In member function `virtual void visitable<T, B>::accept(visitor&)':
  3. main.cpp:12: error: invalid use of undefined type `struct visitor'
  4. main.cpp:5: error: forward declaration of `struct visitor'
  5.  


Any ideas, fellars?

 
Betelgeuse

March 01, 2005, 06:04 PM

The point was so that I could pull shit like:

  1.  
  2. class statement: public visitable<statement, ast_node>
  3. {
  4. public:
  5.         statement();
  6.         virtual ~statement();
  7.         statement_type  _StatementType;
  8. };
  9.  


which saves having to write ::accept() a hundred or so times in my ast processor. (I have a gazillion or so datatypes that somehow derive from ast_node, all which have to be visited at some point)

 
Per Vognsen

March 01, 2005, 10:11 PM

You just need to move the code around--this is clearly a dependency problem. Remove the forward declaration of visitor and move the definition of visitor to the top. At the very top put a forward declaration of ast_node. Replace the definition of visitor::dispatch() with a declaration and after the class definitions, you put

  1.  
  2. inline void visitor::dispatch(ast_node *Base) { Base->accept(*this); }
  3.  

 
This thread contains 3 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.