尝试在赋值中创建一个简单的网格类、非左值
我正在实现一个简单的 C++ 网格类。它应该支持的一个函数是通过圆括号访问的,这样我就可以通过编写 mygrid(0,0) 来访问元素。我重载了 () 运算符,并且收到错误消息:“赋值中的非左值”。
我想要做什么:
//main
cGrid<cA*> grid(5, 5);
grid(0,0) = new cA();
摘录我的网格类实现:
模板 网格类 {
private:
T* data;
int mWidth;
int mHeight;
public:
cGrid(int width, int height) : mWidth(width), mHeight(height) {
data = new T[width*height];
}
~cGrid() { delete data; }
T operator ()(int x, int y)
{
if (x >= 0 && x <= mWidth) {
if (y >= 0 && y <= mHeight) {
return data[x + y * mWidth];
}
}
}
const T &operator ()(int x, int y) const
{
if (x >= 0 && x <= mWidth) {
if (y >= 0 && y <= mHeight) {
return data[x + y * mWidth];
}
}
}
代码的其余部分涉及迭代器的实现,不应该是相关的。
I'm implementing a simple C++ grid class. One Function it should support is accessed through round brackets, so that I can access the elements by writing mygrid(0,0). I overloaded the () operator and i am getting the error message: "non-lvalue in assignment".
what I want to be able to do:
//main
cGrid<cA*> grid(5, 5);
grid(0,0) = new cA();
excerpt of my implementation of the grid class:
template
class cGrid
{
private:
T* data;
int mWidth;
int mHeight;
public:
cGrid(int width, int height) : mWidth(width), mHeight(height) {
data = new T[width*height];
}
~cGrid() { delete data; }
T operator ()(int x, int y)
{
if (x >= 0 && x <= mWidth) {
if (y >= 0 && y <= mHeight) {
return data[x + y * mWidth];
}
}
}
const T &operator ()(int x, int y) const
{
if (x >= 0 && x <= mWidth) {
if (y >= 0 && y <= mHeight) {
return data[x + y * mWidth];
}
}
}
The rest of the code deals with the implementation of an iterator and should not be releveant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Bill 所说,运算符不应该是 const。我相信这就是编译错误的原因(即使报告的错误看起来不同)。编译器仅在赋值行遇到错误,因为它是模板类。
需要明确的是,您不能让
const
方法返回对非const
的引用。即,问题是声明
T &operator... const
是非法的。它应该是T &operator...
或const T &operator... const
。当然,你可以两者兼得。编辑:删除
const
没有帮助,因为现在两个方法具有相同的签名(出于解析调用的目的,返回类型不被视为签名的一部分)。被调用的方法是返回T
的方法,而不是返回T &
的方法。摆脱它(或将其替换为返回 const 引用的 const 方法。)As Bill remarked, the operator shouldn't be
const
. I believe this is the reason for the compilation error (even if the error reported seems different). The compiler only encounters the error at the assignment line because it is a template class.To be clear, you can't have a
const
method return a reference to a non-const
. I.e., theproblem is that the declaration
T &operator... const
is illegal. It should be eitherT &operator...
orconst T &operator... const
. Of course you can have both.Edit: Removing the
const
doesn't help because now both methods have the same signature (the return type is not considered part of the signature for purposes of resolving the call). The method being called is the one returningT
, notT &
. Get rid of it (or replace it with aconst
method returning aconst
reference.)这个模板做什么:
如果 x & y 超出范围吗?你应该提出一个例外。您如何在构造函数中为网格分配内存?至于常量性,您需要提供此运算符的两个版本 - 一种返回常量引用的常量操作符,以及一种返回非常量引用的非常量操作符。
编辑:您在运算符中还存在一个相差一错误。编译并运行:
What does this template:
do if x & y are out of range? You should raise an exception. And how are you allocating memory to the grid in the constructor? As regards constness, you need to supply two versions of this operator - a const one that returns a const reference, and a non-const one that returns a non-const reference.
Edit: You also have an off-by one error in the operator. This compiles and runs: