Template class and 'invalid use of incomplete type' error

Pierpaolo Vesce

I have a template Matrix class and I used a std::vector<std::vector<T>> to store data. I need to specialize some methods for std::complex matrix, for example:

template <typename T>
bool Matrix<T>::is_hermitian() const
{
   if (!(*this).is_squared())
      return false;
   for (int r = 0; r < rows_; r++)
      for (int c = 0; c < columns_; c++)
         if (mat[r][c] != mat[c][r])
            return false;
   return true;
}

For the specialized method I thought something like this:

template <typename T>
bool Matrix<std::complex<T> >::is_hermitian() const
{
   if (!(*this).is_squared())
      return false;
   for (int r = 0; r < rows_; r++)
      for (int c = 0; c < columns_; c++)
         if (mat[r][c] != std::conj(mat[c][r]))
            return false;
   return true;
}

But the compiler returns me an error

'invalid use of incomplete type' 

I instantiated at the end of the .cpp file a bunch of class that I could be using in the main program:

template class Matrix<int>;
template class Matrix<double>;
template class Matrix<float>;
template class Matrix< std::complex<float> >;
template class Matrix< std::complex<int> >;

How can I implement one method for all std::complex<T> type?

And if you know how to replace the last two instance with a Matrix< std::complex<T> > sort of thing I will be very thankful.

JeJo

You can apply SFINE(i.e."Substitution Failure Is Not An Error") technique along with function overloading to choose the correct method when T is std::complex in Matrix<T> class instatiation.

Following is the demonstration of the idea: (See example code online live)

#include <type_traits>  // std::enable_if, std::false_type

// traits for checking, T is `std::complex`
template<typename> struct is_std_complex : std::false_type {};
template<typename T> struct is_std_complex<std::complex<T>> : std::true_type {};

// traits for `std::enable_if` helpers
template<typename Type, typename ReType = void>
using EnabledForComplex = typename std::enable_if<is_std_complex<Type>::value, ReType>::type;

template<typename Type, typename ReType = void>
using EnabledNotForComplex = typename std::enable_if<!is_std_complex<Type>::value, ReType>::type;

template<typename T>
class Matrix
{
   // ...members

public:    
   template<typename Type = T>
   auto is_hermitian() const -> EnabledNotForComplex<Type, bool>
   {
      // ... code for non-std::complex types
   }

   template<typename Type = T>
   auto is_hermitian() const->EnabledForComplex<Type, bool>
   {
      // ... code for std::complex types
   }
};

That being said, if you have access to , you could use if constexpr, which will only instantiate the branch, which is true for the case at compile time. (See example code online live)

#include <type_traits> // std::false_type

// traits for checking, T is `std::complex`
template<typename> struct is_std_complex : std::false_type {};
template<typename T> struct is_std_complex<std::complex<T>> : std::true_type {};

template<typename T>
class Matrix
{
   // ...members

public:
   bool is_hermitian() const
   {
      if (!is_squared()) return false;
      if constexpr (is_std_complex<T>::value)
      {
         // ... code for std::complex types
      }
      else
      {
         // ... code for non-std::complex types
      }
      return true;
   }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Incomplete type is not allowed in a class, but is allowed in a class template

From Dev

Invalid use of incomplete type struct std::hash with unordered_map with std::pair of enum class as key

From Dev

Invalid use of incomplete type, reference vs pointer

From Dev

Error: "invalid use of incomplete type ‘RSA {aka struct rsa_st}" in OpenSSL 1.1.0

From Dev

Error with Eigen vector logarithm invalid use of incomplete type

From Dev

"invalid use of incomplete type". Solving circular dependencies

From Dev

static class template member: invalid application of ‘sizeof’ to incomplete type

From Dev

Using incomplete type in a member function of a class template

From Dev

Invalid use of incomplete type "class"

From Dev

error: invalid use of incomplete type 'struct std::hash<>'

From Dev

C++17 optional tree, error: invalid use of incomplete type

From Dev

Invalid use of incomplete type with with 2 related classes

From Dev

Invalid use of incomplete type for class with std::map<int, T> member

From Dev

Error: Variadic template class has incomplete type

From Dev

Qt - invalid use of incomplete type 'class QScrollBar' - Add horizontal scroll bar to text edit widget

From Dev

Forward declaration of class / Invalid use of incomplete type

From Dev

Creating explicitly specialized template class object yields "object has initializer but incomplete type" error

From Dev

Invalid use of incomplete type for partial template specialization c++

From Dev

"invalid use of incomplete type" for const function pointer type as template argument

From Dev

C++ template specialisation friend iterator error: invalid use of incomplete type

From Dev

GCC tuple "invalid use of incomplete type"

From Dev

Invalid use of incomplete type class error when inheriting from class defined externally

From Dev

How do I solve "[Error] invalid use of incomplete type 'class SLLNode'" Linked Lists

From Dev

Invalid use of incomplete type and expected type-specifier before error in C++

From Dev

Qt error Invalid use of incomplete type 'class UI::FrameLess'

From Dev

Why do I get "invalid use of incomplete type", when the class is fully defined?

From Dev

GCC compiler error when dealing with template classes 'error:invalid use of incomplete type template'

From Dev

Invalid use of incomplete type for named template argument

From Dev

Error using Eigen: invalid use of incomplete type ‘const class Eigen

Related Related

  1. 1

    Incomplete type is not allowed in a class, but is allowed in a class template

  2. 2

    Invalid use of incomplete type struct std::hash with unordered_map with std::pair of enum class as key

  3. 3

    Invalid use of incomplete type, reference vs pointer

  4. 4

    Error: "invalid use of incomplete type ‘RSA {aka struct rsa_st}" in OpenSSL 1.1.0

  5. 5

    Error with Eigen vector logarithm invalid use of incomplete type

  6. 6

    "invalid use of incomplete type". Solving circular dependencies

  7. 7

    static class template member: invalid application of ‘sizeof’ to incomplete type

  8. 8

    Using incomplete type in a member function of a class template

  9. 9

    Invalid use of incomplete type "class"

  10. 10

    error: invalid use of incomplete type 'struct std::hash<>'

  11. 11

    C++17 optional tree, error: invalid use of incomplete type

  12. 12

    Invalid use of incomplete type with with 2 related classes

  13. 13

    Invalid use of incomplete type for class with std::map<int, T> member

  14. 14

    Error: Variadic template class has incomplete type

  15. 15

    Qt - invalid use of incomplete type 'class QScrollBar' - Add horizontal scroll bar to text edit widget

  16. 16

    Forward declaration of class / Invalid use of incomplete type

  17. 17

    Creating explicitly specialized template class object yields "object has initializer but incomplete type" error

  18. 18

    Invalid use of incomplete type for partial template specialization c++

  19. 19

    "invalid use of incomplete type" for const function pointer type as template argument

  20. 20

    C++ template specialisation friend iterator error: invalid use of incomplete type

  21. 21

    GCC tuple "invalid use of incomplete type"

  22. 22

    Invalid use of incomplete type class error when inheriting from class defined externally

  23. 23

    How do I solve "[Error] invalid use of incomplete type 'class SLLNode'" Linked Lists

  24. 24

    Invalid use of incomplete type and expected type-specifier before error in C++

  25. 25

    Qt error Invalid use of incomplete type 'class UI::FrameLess'

  26. 26

    Why do I get "invalid use of incomplete type", when the class is fully defined?

  27. 27

    GCC compiler error when dealing with template classes 'error:invalid use of incomplete type template'

  28. 28

    Invalid use of incomplete type for named template argument

  29. 29

    Error using Eigen: invalid use of incomplete type ‘const class Eigen

HotTag

Archive