【问题标题】:Undefined reference to methods of a templated class对模板化类的方法的未定义引用
【发布时间】:2014-07-18 17:04:55
【问题描述】:

我正在尝试创建一个通用的 2D 数组矩阵类,但是对于我的 [] 重载和构造函数,我收到“错误:未定义对 `Zero::StaticMatrix::StaticMatrix(unsigned int, unsigned int)' 的引用”这两个类。

这是一个类定义的粘贴箱。 http://pastebin.com/Yq8fMAdy

附言我正在使用一个朋友助手类,所以我可以使用类似matrix[i][j] 的东西访问_internal 向量,因为没有[][] 重载。

【问题讨论】:

  • 您需要在您的帖子中包含代码,而不是指向它的链接。如果代码太大而无法解决问题,您需要将其缩小到您遇到问题的特定代码。
  • 今天上面的 Pastebin 404,这就是为什么我们要求将代码嵌入问题中。投票结束。

标签: c++ templates typename


【解决方案1】:

这不是模板的前向声明:

class StaticMatrixRow; // forward declaration

这是:

template< class T > class StaticMatrixRow;

您必须先声明StaticMatrixRow,然后才能在StaticMatrix::operator[] 中使用它。声明运算符,然后在StaticMatrixRow的声明/定义后定义。

StaticMatrix::operator[] 应该创建一个新的行迭代器并将其返回,而不是让您返回引用的行迭代器的单个实例。

StaticMatrix::beginStaticMatrix::end 都声明为 const,因此它们应该返回 const_iterator 类型。

拥有StaticMatrix::erase 毫无意义,因为它们会更改_internal 的大小,而不会更改_rows_columns 成员。 StaticMatrix::clear 也不更新这些成员;它应该调用StaticMatrix::resize( 0, 0 )

StaticMatrixRow::operator[] 应该使用(*_internal)[position],因为_internal 是一个指针。

由于StaticMatrixRow 是使用对向量的常量引用进行初始化的,所以_internal 应该是指向常量向量的指针。由于向量是 const,StaticMatrixRow::operator[] 应该返回 const T &amp;

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多