newbie Question on Templates

G

Guest

Hi Folks,

in interface

template<typename T>
class A{
public:
...
template<T2>
T2 additup(const T2&, const T2&);
....
};

in implementation
not sure if this is correct
template<T>
template<T2>
T2 A<T>::additup(const T2& x, const T2& y){
return (x + y);
}


in main()

A<int> A1;
this initializes T...but i am not sure how to use T2
let T2 = std::list<int>::iterator

A1<std::list<int>::iterator> A2;
A2.additup( A2,A2);

not sure if this correct...
any help greatly appreciated

Barry
 
V

Victor Bazarov

in interface

template<typename T>
class A{
public:
...
template<T2>
T2 additup(const T2&, const T2&);
...
};

in implementation
not sure if this is correct
template<T>
template<T2>
T2 A<T>::additup(const T2& x, const T2& y){
return (x + y);
}

Seems fine. Do you get an error or something?
in main()

A<int> A1;
this initializes T...but i am not sure how to use T2
let T2 = std::list<int>::iterator

A1<std::list<int>::iterator> A2;

A1 is an object. You cannot write <> after it (unless you're comparing
it to something using <). Did you mean to simply declare A2 as

std::list<int>::iterator A2;

???
A2.additup( A2,A2);

I strongly doubt that with your implementation of 'additup' the
compiler will allow that. IIRC, iterators cannot be added one to
another.
not sure if this correct...

It wasn't. Now it should be better. Next time post a _complete_ program.

V
 
S

skyrider

thanks for the help

my goal is to have T1 as an int and T2 as list<T1>::iterator
i would like to do the following because i do not think it is necessary to
strap T2 into the class template...maybe i have too.
I would like to use T2 only for member functions...something like this

template<typename T1>
class A{
public:
//ctors and dtors
//now this is what i want to do
template<typename T2>
T2 getT2();
....
private:
T2 _x;
};

and in test.cpp

template<typename T1>
A<T1>::A(){}

template<typename T1>
template<typename T2>
T2 A<T1>::getT2(){
return _x;
}
....


I can get the following to work...but it is not what i want :(
but maybe i have to live with it ?
/****************************************************************/
#ifndef TEST_H
#define TEST_H

using namespace std;
#include <iostream>

template<typename T1, typename T2>
class A {
public:
A();
~A();
T1 getT1();
void setT1(const T1);
T2 getT2();
void setT2(const T2);

private:
T1 _T1; //my goal is to make this an int
T2 _T2; // and this an iterator from either stl vector or list
};

#endif

using namespace std;
#include "test.h"
#include <iostream>

template<typename T1, typename T2>
A<T1,T2>::A(){
_T1 = 10;
_T2 = 10;
}
template<typename T1 , typename T2>
A<T1,T2>::~A(){
_T1 = 0;
_T2 = 0;
}
template<typename T1, typename T2>
void A<T1,T2>::setT1(const T1 x){
_T1 = x;
}
template<typename T1, typename T2>
T1 A<T1,T2>::getT1(){
return _T1;
}
template<typename T1, typename T2>
void A<T1,T2>::setT2(const T2 x){
_T2 = x;
}
template<typename T1, typename T2>
T2 A<T1,T2>::getT2(){
return _T2;
}

//testMain.cpp
#include <iostream>
#include "test.cpp"
using namespace std;

int main(){
A<int, long> b;
b.setT1(123);
cout << b.getT1() << endl;
return 0;
}
 
V

Victor Bazarov

skyrider said:
my goal is to have T1 as an int and T2 as list<T1>::iterator
i would like to do the following because i do not think it is necessary to
strap T2 into the class template...maybe i have too.

First of all, please don't top-post. Thanks.
I would like to use T2 only for member functions...something like this

template<typename T1>
class A{
public:
//ctors and dtors
//now this is what i want to do
template<typename T2>
T2 getT2();
....
private:
T2 _x;

T2 is undefined. Did you mean to make _x to be of type T1?
};

and in test.cpp

template<typename T1>
A<T1>::A(){}

What is _x initialised to? It seems uninitialised or default-initialised
depending on what its type is.
template<typename T1>
template<typename T2>
T2 A<T1>::getT2(){
return _x;
}

So, it's a convertion function. No big deal.
...


I can get the following to work...but it is not what i want :(
but maybe i have to live with it ?

No, you don't have to. You just need to declare _x as

T1 _x;
/****************************************************************/
#ifndef TEST_H
#define TEST_H

using namespace std;
#include <iostream>

template<typename T1, typename T2>
class A {
public:
A();
~A();
T1 getT1();
void setT1(const T1);
T2 getT2();
void setT2(const T2);

private:
T1 _T1; //my goal is to make this an int
T2 _T2; // and this an iterator from either stl vector or list

I am not sure what role an interator plays here.
};

#endif

using namespace std;
#include "test.h"
#include <iostream>

template<typename T1, typename T2>
A<T1,T2>::A(){
_T1 = 10;
_T2 = 10;
}
template<typename T1 , typename T2>
A<T1,T2>::~A(){
_T1 = 0;
_T2 = 0;
}
template<typename T1, typename T2>
void A<T1,T2>::setT1(const T1 x){
_T1 = x;
}
template<typename T1, typename T2>
T1 A<T1,T2>::getT1(){
return _T1;
}
template<typename T1, typename T2>
void A<T1,T2>::setT2(const T2 x){
_T2 = x;
}
template<typename T1, typename T2>
T2 A<T1,T2>::getT2(){
return _T2;
}

//testMain.cpp
#include <iostream>
#include "test.cpp"
using namespace std;

int main(){
A<int, long> b;
b.setT1(123);
cout << b.getT1() << endl;
return 0;
}
[...]

If you just explain what you're *trying to accomplish* instead of posting
non-working "solutions" to your problems, we could all put our heads
together and recommend something...

V
 
B

base407

Victor Bazarov said:
First of all, please don't top-post. Thanks.


T2 is undefined. Did you mean to make _x to be of type T1?
no...this is part of my problem...i want T2 to be and iterator
_x is a list<T1>::iterator
do i have to use typename list<T1>::iterator _x;
or is there a way to use templates other than template<typename T1,
typename T2>

What i am trying to do is create a generic list class.
what i am trying to get away from is statements like the following
template<typename T>
typename list<T>::iterator A<T>::begin();
return _head;
}

I do not know how to make this function generic...i may want to use vector
instead of list.


}

oops

What is _x initialised to? It seems uninitialised or default-initialised
depending on what its type is.


So, it's a convertion function. No big deal.

No, you don't have to. You just need to declare _x as

T1 _x;


I am not sure what role an interator plays here.
it is an example...make it char*
};

#endif

using namespace std;
#include "test.h"
#include <iostream>

template<typename T1, typename T2>
A<T1,T2>::A(){
_T1 = 10;
_T2 = 10;
}
template<typename T1 , typename T2>
A<T1,T2>::~A(){
_T1 = 0;
_T2 = 0;
}
template<typename T1, typename T2>
void A<T1,T2>::setT1(const T1 x){
_T1 = x;
}
template<typename T1, typename T2>
T1 A<T1,T2>::getT1(){
return _T1;
}
template<typename T1, typename T2>
void A<T1,T2>::setT2(const T2 x){
_T2 = x;
}
template<typename T1, typename T2>
T2 A<T1,T2>::getT2(){
return _T2;
}

//testMain.cpp
#include <iostream>
#include "test.cpp"
using namespace std;

int main(){
A<int, long> b;
b.setT1(123);
cout << b.getT1() << endl;
return 0;
}
[...]

If you just explain what you're *trying to accomplish* instead of posting
non-working "solutions" to your problems, we could all put our heads
together and recommend something...

is it ok to post 500 lines of working code?

thanks for the help
 
V

Victor Bazarov

base407 said:
no...this is part of my problem...i want T2 to be and iterator
_x is a list<T1>::iterator
Why?

do i have to use typename list<T1>::iterator _x;

Maybe. Why not?
or is there a way to use templates other than template<typename T1,
typename T2>

I don't understand the question, I am afraid.
What i am trying to do is create a generic list class.

Why? What don't you like about 'std::list'? Why doesn't it suit you?

OK, OK, maybe you need it for the sake of exercise... I understand.
But why do you need the return value to be list<T1>::iterator? If
you need to create your own list, just create it, and that means you
need to program your own iterators there.
what i am trying to get away from is statements like the following
template<typename T>
typename list<T>::iterator A<T>::begin();
return _head;
}

I do not know how to make this function generic...i may want to use
vector instead of list.

Now you confused the hell out of the issue. You want a generic list, but
you *may want to use* std::vector. Why? Are you trying to program some
kind of adapter to a standard container which will look like a list, no
matter what it adapts? Is there any particular reason in that?

Do you see now why I asked you what you were trying to accomplish?

If you just explain what you're *trying to accomplish* instead of
posting non-working "solutions" to your problems, we could all put
our heads together and recommend something...

is it ok to post 500 lines of working code?

Only if it (a) cannot be reduced to the bare minimum and (b) actually
shows *successfully* what you're trying to *accomplish*. If not, then
please don't.

Again, can you simply explain (in English) what you're trying to do
with your template attempts? Please try to avoid generic statements
like "I am trying to learn templates" or "I am studying programming".

We need a clear description of the problem you think you're solving.

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top