jeudi 15 novembre 2018

C++: cannot define member function

Could you please advise why I am getting the error in the code below?

error: cannot define member function ‘Test<int>::Printer::Print’ within ‘Test<int>’

I am using gcc version 8.1.1 and compile the code as g++ -std=c++11.

Although, if I move the definition of function Print under the definition of struct Printer (i.e. making it inline implicitly), the compiler does not produce any error.

#include <iostream>

template <typename Type>
struct TestBase {

 struct Printer {
  template <typename T>
  void Print(const T& t) {
    std::cout << t << std::endl;
  }
 };

};

template <typename Type>
struct Test;

template<>
struct Test<int> : public TestBase<int> {

 struct Printer : public TestBase<int>::Printer {
  template <typename T>
  void Print(int i, const T& t);
 };

 template <typename T>
 void Printer::Print(int i, const T& t) {
  std::cout << i << t << std::endl;
 }

};

int main() {
  Test<int> t;
}

Aucun commentaire:

Enregistrer un commentaire