Unknown data type

Hello,

I have defined a new data type in MyClass, but the compiler complains when I use that data type for the getter function definition.

This is my code

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

  #include <vector>

  template<class T>
  class MyClass 
  {
    using data_t = std::vector<T>;
    private:
    data_t  p_;
    data_t  d_;

    public:
    MyClass();
    ~MyClass();
    void setP(data_t p);
    data_t getP();
  };

  template<class T>
  void MyClass<T>::setP(data_t p)
  {
     P_ = P;
  }

  template<class T>
  data_t MyClass<T>::getP() /*Compiler gives error that data_t is unknown here
  {
     return p_;
  } */


I also defined data_t in the file scope, but it did not help.
Any comments?

Thanks,
mmgh
Last edited on
You need to fully specify the scope of the return type, the same way as you would have to do if you used it from a function that's not part of the class.

1
2
3
4
5
template<class T>
typename MyClass<T>::data_t MyClass<T>::getP() 
{
	return p_;
}
Last edited on
Since a return type is outside the class you need to add the scope:

typename MyClass<T>::data_t MyClass<T>::getP()

Note that typename is also required because well 'MyClass<T>' is a dependent scope.
Thank you so much.
The problem is solved.
Topic archived. No new replies allowed.