Archived

This topic is now archived and is closed to further replies.

templates issue

This topic is 5680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Try defining doSomething in class body. MSVC doesn''t work well with templated member functions.

Share this post


Link to post
Share on other sites
Try using the template type in the function declaration.


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



------
Andrew

Share this post


Link to post
Share on other sites
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!)

Share this post


Link to post
Share on other sites