By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,482 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,482 IT Pros & Developers. It's quick & easy.

Template instanciation Problem

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ik wrote in news:ch********@odak26.prod.google.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
ik wrote:
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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.