Jump to content
  • Advertisement

Archived

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

donbonifacio

templates issue

This topic is 5838 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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!