const *这部分 C++ 的问题二维数组模板类
我正在研究一个二维数组类——唯一给我带来麻烦的部分是当我声明一个常量 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需更改参数规范即可反映
Row
不会更改m_array
:...
Simply change the parameter specification to reflect that
Row
will not changem_array
:…