Template instanciation Problem

I

ik

Hello All,
I am facing problem with classes having some functions as templates. Is
it a limitation of C++ templates not to have class functions as
templates ? I am attaching the code below, I am not able to complie. I
tried with VC++ and MingW. Can somebody help me ?

#include <iostream>

#include <string>
#include <vector>


class A {
public:

A();
~A();

template <typename T>
void foo(const std::wstring & name, std::vector<T> const &
vecAttrValues);

};


A::A()
{
}

A::~A()
{
}

template <typename T>
void A::foo(const std::wstring & name, std::vector<T> const &
vecAttrValues)
{
std::vector<T>::const_iterator it;
std::vector<T>::const_iterator itEnd = vecAttrValues.end();

for(it = vecAttrValues.begin(); it != itEnd; it++)
{
// Some thing
}
}

template <typename T>
void foog(const std::wstring & name, std::vector<T> const &
vecAttrValues)
{
std::vector<T>::const_iterator it;
std::vector<T>::const_iterator itEnd = vecAttrValues.end();

for(it = vecAttrValues.begin(); it != itEnd; it++)
{
// Some thing
}
}


int main(int argc, char* argv[])
{
std::wstring wstrName(L"My Name");
std::vector<int> vecInts;
A a;
a.foo (wstrName, vecInts);

foog (wstrName, vecInts);
return 0;
}

Note that foog which is the global version of foo, will compile with
out any problem, but foo cannot be compiled.

Thanks
IK.
 
R

Rob Williscroft

ik wrote in in comp.lang.c++:
std::vector<T>::const_iterator it;

You're missing typename before the above:

typename std::vector<T>::const_iterator it;

There are 3 other places this is required.

Your code will not compile with g++ (MingW) as it uses std::wstring
g++ doesn't support this type (It should, there is nothing wrong with
using std::wstring is standard conforming code).

With the typename fixes your code compiled fine with VC++ 7.1, if your
using an earlier version get 7.1, the compiler (not the IDE) is a free
(as in beer) download.

Please indent your code, or get a news reader/email client that
doesn't deindent it for you.

HTH.

Rob.
 
J

Janusz Szpilewski

ik said:
Hello All,
I am facing problem with classes having some functions as templates. Is
it a limitation of C++ templates not to have class functions as
templates ? I am attaching the code below, I am not able to complie. I
tried with VC++ and MingW. Can somebody help me ?
template <typename T>
void A::foo(const std::wstring & name, std::vector<T> const &
vecAttrValues)
{
std::vector<T>::const_iterator it;
std::vector<T>::const_iterator itEnd = vecAttrValues.end();

for(it = vecAttrValues.begin(); it != itEnd; it++)
{
// Some thing
}
}

When in a template definition you refer to a type name defined within
the definition of a template parameter you must tell it to compiler
directly with the typename keyword. Otherwise you might refer to a
member variable what is not allowed. So write:

typename std::vector<T>::const_iterator it; // etc.


Regards,
Janusz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top