problems with templated methods of templated classes

Dear all,

I am struggling on a project in which I have two templated classes, and templated methods. Some methods of the first class take in input objects of the second class, and viceversa, so those methods need to be templated.

The problem occurs when some template method of class A tries to call a templated method of class B.

I have created a small project, to reproduce the same problem in a simplified environment, which code is reported below.

The troubles come in the method "strange()" of class B, precisely when it tries to call the method "makeB()" of class A. Then I get an error from the compiler.

However, if the same method "makeB()" is called from "main()", it works fine.

Notice that everything is on the same file, therefore there are no issues of separating the declaration from the definition of the methods.

Could anyone help me in understanding what I do wrong?
I compile it with linux g++


Thank you all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>

//declaration of the classes

template <class T> class A;
template <class U> class B;


//declaration of the methods of the classes

template <class T>
class A
{
public:
  T status;
  void talk()  {std::cout << "Obj of A. Status = " << status << std::endl;};
  template <class U> void talkB(B<U> other) {std::cout << "Obj of A. Status of OTHER = " << other.status << std::endl;};
  template <class U> B<U> makeB();
};

template <class U>
class B
{
public:
  U status;
  void talk()  {std::cout << "Obj of B. Status = " << status << std::endl;};
  template <class T> void talkA(A<T> other);
  template <class T> void strange(A<T> arg);
};


//definition of the methods of the class A

template<class T> template<class U>
B<U> A<T>::makeB()
{
  B<U> b;
  b.status = U(status)*5;
  return b;
}

//definition of the methods of the class B

template <class U> template <class T>
void B<U>::talkA(A<T> other)
{std::cout << "I'm an obj of B. Status of OTHER = " << other.status << std::endl;}


//Here is where I get troubles.
template <class U> template <class T>
void B<U>::strange(A<T> other)
{
  B<U> x = other.makeB<U>();
  return;
}


//main

int main()
{
  A<int> a;
  a.status = 45;
  B<double> b;
  b.status = 4.3;

  a.talk();
  a.talkB(b);
  b.talk();
  b.talkA(a);


  B<double> b0 = a.makeB<double>();
  b0.talk();


  b0.strange(a);
 


  return 0;
}
Tell the compiler that it is a template.

1
2
3
4
5
6
7
8
9
template <class U> template <class T>
void B<U>::strange(A<T> other)
{

    // B<U> x = other.makeB<U>();
    B<U> x = other. template makeB<U>();

    return;
}
Hello JLBorges,

I thank you very much, now it works.

I was not aware of that syntax. Where can I read about it?


Topic archived. No new replies allowed.