templates issue

Started by
4 comments, last by donbonifacio 21 years, 9 months ago
Hi, I am writing a game and compiling it with VC++ and G++. Until now I have not had many troubles, but today, when I needed some templates runing around, I came across some anoyng errors in VC++. In G++ the fowling compile just fine.
    
class A {
 public:
    
    template<typename Container>
    void doSomething( Container& c );
};

template<typename Container>
void A::doSomething( Container& c ) {c.push_back(1);}
    
And I use it like this:
  
A a;
vector<int> v;
a.doDomething(v);
  
Like I said, this compiles on G++ but not on VC++. Can someonte tell me why? [edited by - Pedro Santos on June 30, 2002 5:13:15 PM]
Advertisement
Try defining doSomething in class body. MSVC doesn''t work well with templated member functions.
---visit #directxdev on afternet <- not just for directx, despite the name
Try using the template type in the function declaration.


  template<typename Container>void A<Container>::doSomething( Container& c ) {c.push_back(1);}  



------
Andrew
As said above, with VC++, member function of templated classes must be declared within the body of the class. It is a known defect.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
As said above, with VC++, member function of templated classes must be declared within the body of the class. It is a known defect.

Actually, they must only be defined within the same file:

  template < typename T >struct A{  void DoSomething( T & t );};template < typename T >void A< T >::DoSomething( T & t ){  t.push_back( 1 );}#include <iostream>#include <vector>int main(){  using namespace std;  vector<int> v;  A< vector<int> > a;  a.DoSomething( v );  cout << v[0] << endl;  return 0;}  

MSVC6, no service packs (hey, I''m not at home!)
Putting the definition on the class body resolved the problem.

Tankz guys!

This topic is closed to new replies.

Advertisement