c++ - 使用 QGenericMatrix 作为另一个类的属性

标签 c++ qt

Qt 有一个名为QGenericMatrix 的类。它的定义如下:

template <int N, int M, typename T>
class QGenericMatrix
{...}

我想在我自己的 MyClass 类中将它用作属性 colorMatrix ,然后像这样在其构造函数中初始化它:

MyClass::MyClass(int n, int m)
{
  colorMatrix = new QGenericMatrix<n, m, QColor>;
}

但是语法可能非常不正确。头文件和构造函数中的声明应该怎么写?

最佳答案

由于模板参数必须在编译时已知,您有两个选择:

  1. 修复 MyClass 中的矩阵大小和类型。例如

    class MyClass {
      QGenericMatrix<2, 3, QColor> colorMatrix;
    }
    
  2. 将 MyClass 定义为采用与 QGenericMatrix 相同的模板参数的模板,并使用 MyClass 的模板参数实例化 colorMatrix。像这样:

    template<int N, int M, typename T>
    class MyClass {
      MyClass() { // init }
      T entry(int i, int j);
    
      QGenericMatrix<N, M, T> colorMatrix;
    }
    
    template<int N, int M, typename T>
    T MyClass::entry(int i, int j) { return colorMatrix(i, j); }
    

    当然,在这种情况下,MyClass 必须使用模板参数进行实例化,这些模板参数必须在编译时再次已知。

    MyClass<2, 3, QColor> myClass;
    

关于c++ - 使用 QGenericMatrix 作为另一个类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375002/

相关文章:

c++ - 与 C++ unordered_map 的并行性

c++ - 尾随零的数量

c++ - For-loop 或 std::any_of,我应该使用哪一个?

c++ - 初始化 QByteArray 避免 [-Wnarrowing]

c++ - 添加行主矩阵和列主矩阵

c++ - 放置一个 std::pair

c++ - 将 cocos2d 移植到 Qt

c++ - (Qt C++) int 不会加数字吗?

c++ - 如何通过SMTP发送邮件并使用Qt获取邮件?

qt - 使用 Qt、VC++ 和 OpenCV 库时出错