Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;部分模板实例化中的Typedef 我很难理解为什么我的部分实例化模板不是在C++中编译的。_C++_Templates_Typedef_Partial Specialization - Fatal编程技术网

C++;部分模板实例化中的Typedef 我很难理解为什么我的部分实例化模板不是在C++中编译的。

C++;部分模板实例化中的Typedef 我很难理解为什么我的部分实例化模板不是在C++中编译的。,c++,templates,typedef,partial-specialization,C++,Templates,Typedef,Partial Specialization,我试图提供一个类,该类根据传递给它的模板返回不同的类型: enum EVectorType { eVectorType_Scalar, eVectorType_Vector, eVectorType_Matrix }; template< EVectorType kVectorTypeOne, EVectorType kVectorTypeTwo, typename TypeOne, typename TypeTwo> class MultSwitch

我试图提供一个类,该类根据传递给它的模板返回不同的类型:

enum EVectorType {
  eVectorType_Scalar,
  eVectorType_Vector,
  eVectorType_Matrix
};

template<
  EVectorType kVectorTypeOne,
  EVectorType kVectorTypeTwo,
  typename TypeOne,
  typename TypeTwo>
class MultSwitch {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;
 public:
  typedef TypeOne ResultType;
  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return m_A * m_B; }
};
以下专业按预期工作:

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Scalar,
  eVectorType_Vector,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef TypeTwo ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return ScalarMultiply(m_B, m_A); }
};

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Vector,
  eVectorType_Scalar,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef TypeOne ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return ScalarMultiply(m_A, m_B); }
};

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Vector,
  eVectorType_Vector,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef typename TypeOne::ScalarType ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return m_A.Dot(m_B); }
};
产生以下错误:

TestMatrix.cpp:145:42: error: conversion from ‘MultSwitch<(EVectorType)2u, (EVectorType)2u, MatrixBase<int, 2, 3>, MatrixBase<int, 3, 5> >::ResultType {aka MatrixBase<int, 2, 3>}’ to non-scalar type ‘MatrixBase<float, 2, 5>’ requested
   MatrixBase<float, 2, 5> amb = a * b;
TestMatrix.cpp:145:42:错误:请求从'MultSwitch::ResultType{aka MatrixBase}'转换为非标量类型'MatrixBase'
矩阵基amb=a*b;

我认为您有两个问题:

  • 在MatrixBase,我想

    static const int kNumCols = nRows;
    
    应该是

    static const int kNumCols = nCols;
    
  • “a*b”将返回a

    MatrixBase<int, 2, 5>
    
    MatrixBase
    
    类型,而不是

    MatrixBase<float, 2, 5>
    
    MatrixBase
    
    您需要添加一个复制构造函数来执行int->float转换


  • 我相信您有两个问题:(1)在MatrixBase中,我认为“static const int kNumCols=nRows;”应该是“static const int kNumCols=nCols;”(2)“a*b”将返回MatrixBase,而不是MatrixBase。您需要添加一个复制构造函数来执行int->float转换。是的,我刚刚捕捉到了它。添加它作为一个答案!我已经有了一个用于此的副本构造函数。:)
    static const int kNumCols = nCols;
    
    MatrixBase<int, 2, 5>
    
    MatrixBase<float, 2, 5>