【问题标题】:Use a template inside a template that have the same type在具有相同类型的模板内使用模板
【发布时间】:2019-09-11 19:06:52
【问题描述】:

我正在为学校做一个项目,但我遇到了模板问题:我想在其构造函数中使用与我的实际模板相同类型的模板,但我遇到了这个错误:声明与“我的构造函数”不兼容.

代码如下:

template <typename Type> class CMatrixIterator : public CIterator
{
      CMatrixIterator(CMatrix<Type>& MATmatrice);
}

template <typename Type>
inline CMatrixIterator<Type>::CMatrixIterator(CMatrix<Type>& MATmatrice) : iIndex(0), MATmatrice(MATmatrice)
{
}

以及完整的错误:

declaration incompatible with "CMatrixIterator<Type>::CMatrixIterator(<error-type> & MATmatrice)

我不明白为什么我会收到此错误,有人知道吗? 谢谢。

完整代码:

#pragma once
#include "CIterator.h"
#include "CMatrix.h"

template <typename Type> class CMatrixIterator : public CIterator
{
    private:
        int iIndex;
        CMatrix<Type>& MATmatrice;

    public:
        CMatrixIterator(CMatrix<Type>& MATmatrice);
        ~CMatrixIterator();

        virtual Type ITEfirst();
        virtual bool ITEisDone();
        virtual void ITEnext();
        virtual Type ITEcurrentItem();
};



template <typename Type>
inline CMatrixIterator<Type>::CMatrixIterator(CMatrix<Type>& MATmatrice) : iIndex(0), MATmatrice(MATmatrice)
{
}

template<typename Type>
inline CMatrixIterator<Type>::~CMatrixIterator()
{
}

template<typename Type>
inline Type CMatrixIterator<Type>::ITEfirst()
{
    return MATmatrice.getValues(0, 0);
}

template<typename Type>
inline bool CMatrixIterator<Type>::ITEisDone()
{
    return MATmatrice.MATrows * MATmatrice.MATcols() > index ? true : false;
}

template<typename Type>
inline void CMatrixIterator<Type>::ITEnext()
{
    iIndex++;
}

template<typename Type>
inline Type CMatrixIterator<Type>::ITEcurrentItem()
{
    return MATmatrice.MATgetValues(iIndex % MATmatrice.MATcols, iIndex / MATmatrice.MATcols);
}

【问题讨论】:

  • 如果CMatrix&lt;T&gt; 在您到达定义构造函数的位置时声明?这可能是一个周期性的头依赖问题。
  • 我的文件顶部有#pragma once #include "CIterator.h" #include "CMatrix.h"
  • 无法重现,请参阅:wandbox.org/permlink/oE99ZvcRkjJQQzqY,但这是使用 gcc。下次请发minimal reproducible example
  • 也无法使用 MSVC 进行复制,请参阅:rextester.com/QKWMW15635
  • 感谢您的帮助,我编辑了我的第一篇文章。它似乎适用于您的链接,也许是视觉工作室的问题?

标签: c++ visual-studio templates


【解决方案1】:

如果 CMatrix 在您到达定义构造函数的位置时声明?这可能是一个周期性的头依赖问题。

CIterator.h 或 CMatrix.h 是否包含包含您显示的代码的文件?

弗朗索瓦·安德里厄。

【讨论】:

    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 2014-08-19
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多