How to use this member template

N

nin234ATIyahoo.com

Here is a stripped down version of what I am trying to do. I would like
to know how to use this template member function
class B
{
int b;
public:
B() {};
template<class DbTags, class DbNams> int getTgs(int &, DbTags&,
DbNams&);

};


template<class DbTags, class DbNams> int B::getTgs(int& a, DbTags& oTg,
DbNams& oNm)
{
typename DbTags::iterator ip;
typename DbNams::iterator in;
for (ip = oTg.begin(); ip != oTg.end(); ip++)
std::cout << (*ip) << std::endl;
//copy (oTg.begin(), oTg.end(),
std::eek:stream_iterator<int>(std::cout));
for (in = oNm.begin(); in != oNm.end(); in++)
std::cout << (*in) << std::endl;
}

template<class DbTg> class A
{
int a;
public:
A();

};
template<class DbTg> A::A()
{
B myB;
int f=9;
DbTg oT;
DbNams oN;
oN.push_back("irq_time");
oN.push_back("irq_wait");
oT.push_back(5);
oT.push_back(6);
myB.getTgs(f, oT, oN);
}


int main()
{
A<std::list<int> > myA;

}

My question is how to use B::getTgs in the constructor A. When the
B::getTgs has the first two arguments only I am able to use it. It is
the third argument that is creating the problem

Regards
Ninan
 
V

Victor Bazarov

nin234ATIyahoo.com said:
Here is a stripped down version of what I am trying to do. I would like
to know how to use this template member function
class B
{
int b;
public:
B() {};
template<class DbTags, class DbNams> int getTgs(int &, DbTags&,
DbNams&);

};


template<class DbTags, class DbNams> int B::getTgs(int& a, DbTags& oTg,
DbNams& oNm)
{
typename DbTags::iterator ip;
typename DbNams::iterator in;
for (ip = oTg.begin(); ip != oTg.end(); ip++)
std::cout << (*ip) << std::endl;
//copy (oTg.begin(), oTg.end(),
std::eek:stream_iterator<int>(std::cout));
for (in = oNm.begin(); in != oNm.end(); in++)
std::cout << (*in) << std::endl;
}

template<class DbTg> class A
{
int a;
public:
A();

};
template<class DbTg> A::A()
{
B myB;
int f=9;
DbTg oT;
DbNams oN;
oN.push_back("irq_time");
oN.push_back("irq_wait");
oT.push_back(5);
oT.push_back(6);
myB.getTgs(f, oT, oN);
}


int main()
{
A<std::list<int> > myA;

}

My question is how to use B::getTgs in the constructor A. When the
B::getTgs has the first two arguments only I am able to use it. It is
the third argument that is creating the problem

What problem? I mean, aside from the fact that in the code above,
in the constructor of A, 'DbNams' is undefined, I don't see any
problem. Perhaps you could elaborate on what you're trying to
achieve... Maybe (and this is a pure speculation) you meant to
declare 'oN' a list of something, or maybe (another speculation)
you wanted to add another argument to your 'A' template...

V
 
N

ninant

Victor said:
What problem? I mean, aside from the fact that in the code above,
in the constructor of A, 'DbNams' is undefined, I don't see any
problem. Perhaps you could elaborate on what you're trying to
achieve... Maybe (and this is a pure speculation) you meant to
declare 'oN' a list of something, or maybe (another speculation)
you wanted to add another argument to your 'A' template...

V
 
N

ninant

Let me clarify this
This works if I change the code like this
template<class DbTg, class DbNams> class A
{
int a;
public:
A();

};
template<class DbTg, class DbNams> A<DbTg, DbNams>::A()
{
B myB;
int f=9;
DbTg oT;
DbNams oN;
oN.push_back("irq_time");
oN.push_back("irq_wait");
oT.push_back(5);
oT.push_back(6);
myB.getTgs(f, oT, oN);
}

int main()
{
A<std::list<int>, std::list<std::string> > myA;

}
But here I am declaring DbNams template for the entire class, whereas
it is just a local variable inside the constructor. Is there a way to
define this template (DbNams) just for the constructor
 
V

Victor Bazarov

[...]
But here I am declaring DbNams template for the entire class, whereas
it is just a local variable inside the constructor. Is there a way to
define this template (DbNams) just for the constructor

Don't call it DbNams. Give it some real type. And don't top-post.
 
N

Ninan Thomas

Victor said:
[...]
But here I am declaring DbNams template for the entire class, whereas
it is just a local variable inside the constructor. Is there a way to
define this template (DbNams) just for the constructor

Don't call it DbNams. Give it some real type. And don't top-post.

That is obvious. What I wanted was a member template constructor. I
read from older posts in this NG (posted couple of years ago) that it
is not possible. Is it still true
 
V

Victor Bazarov

Ninan Thomas said:
Victor said:
[...]
But here I am declaring DbNams template for the entire class, whereas
it is just a local variable inside the constructor. Is there a way to
define this template (DbNams) just for the constructor

Don't call it DbNams. Give it some real type. And don't top-post.

That is obvious. What I wanted was a member template constructor. I
read from older posts in this NG (posted couple of years ago) that it
is not possible. Is it still true

A template constructor in not possible if the template argument is not
among the constructor's arguments. But if it's one of the arguments,
why not?

struct A {
template<class T> A(T blah);
};

int main() {
A a(42); // T is 'int'
}

V
 

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
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top