const *这部分 C++ 的问题二维数组模板类

发布于 2024-10-29 11:59:38 字数 2047 浏览 1 评论 0原文

我正在研究一个二维数组类——唯一给我带来麻烦的部分是当我声明一个常量 Array2D 时。 [] 运算符将对 &this 的引用传递给 Row 构造函数。当我尝试使用常量 Array2D 执行此操作时,出现以下错误消息:


error C2665: 'Row<T>::Row' : none of the 2 overloads could convert all the argument types
with
[
   T=int
]

row.h(14): could be 'Row<T>::Row(Array2D<T> &,int)'


with
[
    T=int
]
 while trying to match the argument list '(const Array2D<T>, int)'
 with
[
    T=int
]

array2d.h(87) : while compiling class template member function 'Row<T> Array2D<T>::operator [](int) const'
with
[
T=int
]

main.cpp(30) : see reference to class template instantiation 'Array2D<T>' being compiled
 with
[
 T=int
]
row.h(34): error C2662: 'Array2D<T>::Select' : cannot convert 'this' pointer from 'const Array2D<T>' to 'Array2D<T> &'
 with

T=int Conversion loses qualifiers
\row.h(33) : while compiling class template member function 'int &Row<T>::operator [](int)'
 with
    T=int

main.cpp(35) : see reference to class template instantiation 'Row<T>' being compiled
with
 [
T=int
]

好的,这是代码。我知道问题与将常量 Array2D 的 *this 指针传递给 Row 构造函数有关,但我一生都无法找出解决方案。

任何帮助将不胜感激。

//from array2d.h
template <typename T>
Row<T> Array2D<T>::operator[](int row) const
{
if(row >= m_rows)
    throw MPexception("Row out of bounds");

return Row<T>(*this , row);

}

//from row.h
template <typename T>
class Row
{ 
public:
    Row(Array2D<T> & array, int row);
    T operator [](int column) const;
    T & operator [](int column);
private:
    Array2D<T> & m_array2D;
    int m_row;
};
template <typename T>
Row<T>::Row(Array2D<T> & array, int row) : m_row(row), m_array2D(array)
{}

template <typename T>
T Row<T>::operator[](int column) const
{
    return m_array2D.Select(m_row, column);
}

template <typename T>
T & Row<T>::operator[](int column)
{
return m_array2D.Select(m_row, column);
}

I'm working on a 2d array class -- and the only portion that is giving me trouble is when I declare a constant Array2D. The [] operator passes a reference to &this to the Row constructor. When I try to do this with a constant Array2D, I'm given the following error message:


error C2665: 'Row<T>::Row' : none of the 2 overloads could convert all the argument types
with
[
   T=int
]

row.h(14): could be 'Row<T>::Row(Array2D<T> &,int)'


with
[
    T=int
]
 while trying to match the argument list '(const Array2D<T>, int)'
 with
[
    T=int
]

array2d.h(87) : while compiling class template member function 'Row<T> Array2D<T>::operator [](int) const'
with
[
T=int
]

main.cpp(30) : see reference to class template instantiation 'Array2D<T>' being compiled
 with
[
 T=int
]
row.h(34): error C2662: 'Array2D<T>::Select' : cannot convert 'this' pointer from 'const Array2D<T>' to 'Array2D<T> &'
 with

T=int Conversion loses qualifiers
\row.h(33) : while compiling class template member function 'int &Row<T>::operator [](int)'
 with
    T=int

main.cpp(35) : see reference to class template instantiation 'Row<T>' being compiled
with
 [
T=int
]

Okay, and here's the code. I know the problem has to do with passing the *this pointer of the constant Array2D to the Row constructor, but I can't for the life of me figure out the solution.

Any help would be greatly appreciated.

//from array2d.h
template <typename T>
Row<T> Array2D<T>::operator[](int row) const
{
if(row >= m_rows)
    throw MPexception("Row out of bounds");

return Row<T>(*this , row);

}

//from row.h
template <typename T>
class Row
{ 
public:
    Row(Array2D<T> & array, int row);
    T operator [](int column) const;
    T & operator [](int column);
private:
    Array2D<T> & m_array2D;
    int m_row;
};
template <typename T>
Row<T>::Row(Array2D<T> & array, int row) : m_row(row), m_array2D(array)
{}

template <typename T>
T Row<T>::operator[](int column) const
{
    return m_array2D.Select(m_row, column);
}

template <typename T>
T & Row<T>::operator[](int column)
{
return m_array2D.Select(m_row, column);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2024-11-05 11:59:38

只需更改参数规范即可反映 Row 不会更改 m_array

Row(Array2D<T> const & array, int row); // argument is read-only

...

Row<T>::Row(Array2D<T> const & array, int row) : m_row(row), m_array2D(array)

Simply change the parameter specification to reflect that Row will not change m_array:

Row(Array2D<T> const & array, int row); // argument is read-only

Row<T>::Row(Array2D<T> const & array, int row) : m_row(row), m_array2D(array)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文